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')
}
相关推荐
崔庆才丨静觅8 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60619 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了9 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅9 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅10 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅10 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment10 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅10 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊10 小时前
jwt介绍
前端
爱敲代码的小鱼11 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax