vue3项目搭建手册

Vue 3 + TypeScript + Vite + Element-Plus

环境支持

由于 Vue 3 不再支持 IE11,Element Plus 也不再支持 IE 浏览器。
Edge ≥ 79 Firefox ≥ 78 Chrome ≥ 64 Safari ≥ 12

1. 使用vite安装Vue3

npm create vite@latest my-vue-app -- --template vue-ts

2. 安装element-plus,并引入

npm install element-plus --save
// main.ts
import { createApp } from 'vue'
import './style.css'
++ import ElementPlus from 'element-plus'
++ import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

++ app.use(ElementPlus)
app.mount('#app')

3. vscode关于vue3+ts的插件

Volar为vue3开发,vetur主要是vue2,如果共存,就会报“has no default export”的错误

  1. Vue Language Features (Volar)——vue3的代码高亮及语法提示
  2. TypeScript Vue Plugin (Volar)——支持ts的vue3代码提示
  3. Vue VSCode Snippets——快速生成各种代码片段

4. 配置vite.config.ts(含修改端口方式)

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    port: 8080,
  }
})

5. 找不到模块“vue”或其相应的类型声明——解决

发现只是没有@types/node声明文件包产生的问题

  • 安装 @types/node 文件包:
npm i --save-dev @types/node
  • 在typescript对应的编译配置文件tsconfig.json写入以下代码:
"compilerOptions": {
    ...
 
    "types": [
      "node"
    ],
  },

6.vite vue-ts 配置 “@” 路径别名

  • 修改vite.config.js
import { defineConfig } from 'vite'
import { resolve } from 'path'

export default defineConfig {
    // ...
    resolve: {
        alias: {
            "@": resolve(__dirname, 'src'), // 路径别名
        },
        extensions: ['.js', '.json', '.ts'] // 使用路径别名时想要省略的后缀名,可以自己 增减
    }
    // ...
}

vite 官方文档中 不建议忽略 .vue 后缀的文,所以在 import 引入文件的时候需要加 .vue

例 import HelloWorld from ‘@/components/HelloWorld.vue’

  • 修改tsconfig.json
{
    "compilerOptions" : {
        // ...
        "baseUrl": ".", // 用于设置解析非相对模块名称的基本目录,相对模块不会受到baseUrl的影响
        "paths": { // 用于设置模块名到基于baseUrl的路径映射
            "@/*": ["src/*"]
        }
        // ...
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King_960725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值