vite搭建项目(vue3+ts+tsx+router+ant-design-vue@next)

vite搭建项目(vue3+ts+tsx+router+ant-design-vue@next)

创建项目

yarn create vite zhijian-work --template vue-ts

下载依赖

# ts支持node的path
yarn add -D @types/node

# 支持jsx
yarn add -D @vitejs/plugin-vue-jsx

# 支持 less
yarn add -D less

# 组件库
yarn add ant-design-vue@next

# 按需引入样式
vite-plugin-style-import

# 支持路由
yarn add vue-router@next

创建文件夹和文件,删除默认

rm -rf src/components/HelloWorld.vue src/assets/logo.png
mkdir style
touch style/global.less
mkdir router
touch router/index.ts

配置

vite.config.js

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import styleImport from 'vite-plugin-style-import';

import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      // 别名
      '@': path.resolve(__dirname, 'src'),
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          // 全局less变量
          hack: `true; @import (reference) "${path.resolve(
            'src/style/global.less'
          )}";`,
        },
        javascriptEnabled: true,
      },
    },
  },
  plugins: [
    vue(),
    vueJsx({
      // options are passed on to @vue/babel-plugin-jsx
      transformOn: true,
      mergeProps: true,
    }),
    // 按需引入样式
    styleImport({
      libs: [
        {
          libraryName: 'ant-design-vue',
          esModule: true,
          resolveStyle: (name) => {
            return `ant-design-vue/es/${name}/style/index`;
          },
        },
      ],
    }),
  ],
});

main.ts

import { createApp } from 'vue';
import App from './App';
import router from './router';

createApp(App).use(router).mount('#app');

router/index.ts

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';

const routes: RouteRecordRaw[] = [
  {
    // 首页
    path: '/',
    name: 'DraggblePage',
    component: () =>
      import(
        /* webpackChunkName: "DraggblePage" */ '@/views/pages/DraggblePage'
      ),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, form, next) => {
  next();
});

export default router;

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "srsx", "src/**/*.v.github91e"]
}

目录结构

在这里插入图片描述

Github源码

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值