Vue3 工程创建常用步骤

使用 vite 创建项目

lua 复制代码
pnpm create vite my-vue-app --template vue

安装 typescript

csharp 复制代码
pnpm add typescript -g
// 创建配置文件
tsc --init 

常用配置

ruby 复制代码
{
  "compilerOptions": {
    "target": "esnext", // 编译后的 js 语法版本 [es5,es6,es7,esnext]
    "module": "esnext", // 编译后的 js 模块版本 [commonjs (服务端), esmodule(客户端)]
    "strict": true,     // 是否严格模式
    "jsx": "preserve",  // 编译后 jsx 模式 [preserve, react, react-native]
    "allowJs": true,    // 是否编译js文件
    "moduleResolution": "node", // 模块解析策略 [node,classic]
    "skipLibCheck": true,  // 是否跳过声明文件类型检查 
    "esModuleInterop": true,  // esm 是否可以引入 符合 esm规范的 commonjs 模块
    "allowSyntheticDefaultImports": true, 是否可以引入没有默认导出的模块
    "forceConsistentCasingInFileNames": true, // 是否强制模块文件名和文件系统名一致
    "experimentalDecorators": true, // 是否可以使用装饰器
    "useDefineForClassFields": true, // class属性是否编译成js文件中
    "noImplicitAny": false, // 有隐含的 any类型时是否报错
    "sourceMap": true,  // 是否生成 sourceMap 文件
    "baseUrl": ".",  // 非
    "types": ["webpack-env", "node"],
    "paths": {  // 别名
      "@/*": ["src/*"]
    },
    "lib": ["esnext", "dom", "dom.iterable", "scripthost"]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx",
    "auto-imports.d.ts"
  ],
  "exclude": ["node_modules"]
}

修改 index.html 入口文件引入

xml 复制代码
<script type="module" src="/src/main.ts"></script>

添加 vue 声明文件 shim.d.ts

typescript 复制代码
declare module "*.vue" {
  import type { DefineComponent } from "vue";
  const component: DefineComponent<{}, {}, any>;
  export default component;
}

安装 tailwindcss

pnpm install -D tailwindcss postcss autoprefixer

创建 postcss.config.js

css 复制代码
module.exports = {
    plugins: {
        autoprefixer: {},
        tailwindcss: {},
    },
}

创建 tailwind.config.js

css 复制代码
//tailwind.config.js
module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

创建 tailwind.css

less 复制代码
@tailwind base;
@tailwind components;
@tailwind utilities;

vite.config.ts 引入

javascript 复制代码
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
  },
  css: {
    postcss: {
      plugins: [tailwindcss, autoprefixer],
    },
  },
});

安装vue-router

sql 复制代码
pnpm add vue-router@4

创建 routers 文件夹 与 index.ts

typescript 复制代码
import { createRouter, createWebHashHistory } from "vue-router";
const routes: any = [
  {
    path: "/",
    component: () => import("@/views/index.vue"),
  },
];
export default createRouter({
  history: createWebHashHistory(),
  routes,
});

main.ts 引入

javascript 复制代码
import router from "@/routers/index";
app.use(router);

添加 ant-design-vue

css 复制代码
pnpm add --save ant-design-vue@4.

main.ts 添加 antd

javascript 复制代码
import antd from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';

app.use(antd)

解决 tailwind.css 与 antd 样式冲突

java 复制代码
//tailwind.config.js
module.exports = {

  ....
  corePlugins: {
    preflight: false,
  },
}

测试效果:

xml 复制代码
<template>
    <div class="flex justify-center h-screen items-center">
        <a-button type="primary">dasdsa</a-button>
    </div>
</template>

<script setup>

</script>

<style lang="scss" scoped></style>

自动引入 composition API

arduino 复制代码
pnpm i -D unplugin-auto-import

vite.config.ts 添加 plugin

css 复制代码
import AutoImport from "unplugin-auto-import/vite";

plugins: [
  vue(),
   AutoImport({
    imports: ["vue", "vue-router"], // 自动导入vue和vue-router相关函数
    dts: "src/auto-import.d.ts", // 生成 auto-import.d.ts 全局声明   
  }),
],

生成auto-import.ts

php 复制代码
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
export {}
declare global {
  const EffectScope: typeof import('vue')['EffectScope']
  const computed: typeof import('vue')['computed']
  const createApp: typeof import('vue')['createApp']
  const customRef: typeof import('vue')['customRef']
  const defineAsyncComponent: typeof import('vue')['defineAsyncComponent']
  const defineComponent: typeof import('vue')['defineComponent']
  const effectScope: typeof import('vue')['effectScope']
  const getCurrentInstance: typeof import('vue')['getCurrentInstance']
  const getCurrentScope: typeof import('vue')['getCurrentScope']
  const h: typeof import('vue')['h']
  const inject: typeof import('vue')['inject']
  const isProxy: typeof import('vue')['isProxy']
  const isReactive: typeof import('vue')['isReactive']
  const isReadonly: typeof import('vue')['isReadonly']
  const isRef: typeof import('vue')['isRef']
  const markRaw: typeof import('vue')['markRaw']
  const nextTick: typeof import('vue')['nextTick']
  const onActivated: typeof import('vue')['onActivated']
  const onBeforeMount: typeof import('vue')['onBeforeMount']
  const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave']
  const onBeforeRouteUpdate: typeof import('vue-router')['onBeforeRouteUpdate']
  const onBeforeUnmount: typeof import('vue')['onBeforeUnmount']
  const onBeforeUpdate: typeof import('vue')['onBeforeUpdate']
  const onDeactivated: typeof import('vue')['onDeactivated']
  const onErrorCaptured: typeof import('vue')['onErrorCaptured']
  const onMounted: typeof import('vue')['onMounted']
  const onRenderTracked: typeof import('vue')['onRenderTracked']
  const onRenderTriggered: typeof import('vue')['onRenderTriggered']
  const onScopeDispose: typeof import('vue')['onScopeDispose']
  const onServerPrefetch: typeof import('vue')['onServerPrefetch']
  const onUnmounted: typeof import('vue')['onUnmounted']
  const onUpdated: typeof import('vue')['onUpdated']
  const provide: typeof import('vue')['provide']
  const reactive: typeof import('vue')['reactive']
  const readonly: typeof import('vue')['readonly']
  const ref: typeof import('vue')['ref']
  const resolveComponent: typeof import('vue')['resolveComponent']
  const shallowReactive: typeof import('vue')['shallowReactive']
  const shallowReadonly: typeof import('vue')['shallowReadonly']
  const shallowRef: typeof import('vue')['shallowRef']
  const toRaw: typeof import('vue')['toRaw']
  const toRef: typeof import('vue')['toRef']
  const toRefs: typeof import('vue')['toRefs']
  const toValue: typeof import('vue')['toValue']
  const triggerRef: typeof import('vue')['triggerRef']
  const unref: typeof import('vue')['unref']
  const useAttrs: typeof import('vue')['useAttrs']
  const useCssModule: typeof import('vue')['useCssModule']
  const useCssVars: typeof import('vue')['useCssVars']
  const useLink: typeof import('vue-router')['useLink']
  const useRoute: typeof import('vue-router')['useRoute']
  const useRouter: typeof import('vue-router')['useRouter']
  const useSlots: typeof import('vue')['useSlots']
  const watch: typeof import('vue')['watch']
  const watchEffect: typeof import('vue')['watchEffect']
  const watchPostEffect: typeof import('vue')['watchPostEffect']
  const watchSyncEffect: typeof import('vue')['watchSyncEffect']
}
// for type re-export
declare global {
  // @ts-ignore
  export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
  import('vue')
}
相关推荐
web1478621072320 分钟前
C# .Net Web 路由相关配置
前端·c#·.net
m0_7482478021 分钟前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
飞的肖25 分钟前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
青灯文案132 分钟前
前端 HTTP 请求由 Nginx 反向代理和 API 网关到后端服务的流程
前端·nginx·http
m0_7482548837 分钟前
DataX3.0+DataX-Web部署分布式可视化ETL系统
前端·分布式·etl
ZJ_.1 小时前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
GIS开发特训营1 小时前
Vue零基础教程|从前端框架到GIS开发系列课程(七)响应式系统介绍
前端·vue.js·前端框架·gis开发·webgis·三维gis
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
学代码的小前端1 小时前
0基础学前端-----CSS DAY9
前端·css
joan_851 小时前
layui表格templet图片渲染--模板字符串和字符串拼接
前端·javascript·layui