vite+vue3.0+ts+element-plus快速搭建項目的實現(xiàn)
vite 出了 2.x 版本,抱著學一學的心態(tài)決定出個簡單的項目,結合 element-plus,以及將會成為每位前端必會的 typescript,實現(xiàn)了如下內(nèi)容。
vite是一個由原生 ESM 驅動的 Web 開發(fā)構建工具。在開發(fā)環(huán)境下基于瀏覽器原生 ES imports 開發(fā),在生產(chǎn)環(huán)境下基于 Rollup 打包。
vite 作用
- 快速的冷啟動:不需要等待打包操作;
- 即時的熱模塊更新:替換性能和模塊數(shù)量的解耦讓更新飛起;
- 真正的按需編譯:不再等待整個應用編譯完成,這是一個巨大的改變。
使用的環(huán)境
- node v12.19.0
- @vue/cli 4.5.8
搭建項目
npm init vite-app <project-name> cd <project-name> npm install npm run dev
或
yarn create vite-app <project-name> cd <project-name> yarn yarn dev
配置
vite.config.ts
vite.config.ts 相當于 @vue-cli 項目中的 vue.config.js
import path from "path";
const pathResolve = (pathStr: string) => {
return path.resolve(__dirname, pathStr);
}
const config = {
base: './',//在生產(chǎn)中服務時的基本公共路徑。@default '/'
alias: {
'/@/': pathResolve('./src'),
},
outDir: 'vite-init',//構建輸出將放在其中。會在構建之前刪除舊目錄。@default 'dist'
minify: 'esbuild',//構建時的壓縮方式
hostname: 'localhost',//本地啟動的服務地址
port: '8800',//服務端口號
open: false,//啟動服務時是否在瀏覽器打開
https: false,//是否開啟https
ssr: false,//是否服務端渲染
optimizeDeps: {// 引入第三方的配置
include: ['axios']
},
// proxy: {//代理配置
// '/api': {
// target: 'http://xx.xx.xx.xx:xxxx',
// changeOrigin: true,
// ws: true,
// rewrite: (path: string) => { path.replace(/^\/api/, '') }
// }
// }
}
module.exports = config;
tsconfig.json
{
"compilerOptions": {
"target": "ES5",//指定ECMAScript的目標版本:'ES3'(默認),'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020',或'ESNEXT'。
"module": "commonjs",//指定模塊代碼生成:'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020',或'ESNext'。
"strict": true,//是否啟用所有嚴格的類型檢查選項。
"baseUrl":"./",//用于解析非絕對模塊名稱的基本目錄。
"paths": {
"/@/*": ["./src/*"]
},
"noImplicitAny": true, //對于隱含有'any'類型的表達式和聲明引發(fā)錯誤。
"esModuleInterop": true, //通過為所有導入創(chuàng)建名稱空間對象,實現(xiàn)CommonJS和ES模塊之間的互操作性。意味著“allowSyntheticDefaultImports”。
"experimentalDecorators": true, //支持對ES7裝飾器的實驗性支持。
"skipLibCheck": true, //跳過聲明文件的類型檢查。
"forceConsistentCasingInFileNames": true //禁止對同一文件使用大小寫不一致的引用。
}
}
App.vue
修改app.vue
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<router-view />
</template>
<script>
export default {
name: 'App',
setup() {
}
}
</script>
Views
在 src 下新建 views文件夾,并在文件夾內(nèi)創(chuàng)建 index.vue
<template>
<HelloWorld :msg="msg"></HelloWorld>
</template>
<script lang="ts">
import HelloWorld from "/@/views/HelloWorld.vue";
import { defineComponent } from "vue";
export default defineComponent({
name: "Home",
components: { HelloWorld },
setup() {
return {
msg: "hello World",
};
},
});
</script>
PS:在引入.vue文件時一定要帶上后綴名,否則會報找不到文件
在views文件夾內(nèi)創(chuàng)建 HelloWorld.vue
<template>
<h1>{{ msg }}</h1>
<button @click="realTime.count++">count is: {{ realTime.count }}</button>
<button @click="handleclick">點擊跳轉其它路由</button>
<p>Edit <code>components/HelloWorld.vue</code> to test hot module replacement.</p>
</template>
<script>
import { defineComponent, reactive } from "vue";
import { useRouter } from 'vue-router'
export default defineComponent({
name: 'Index',
props: { msg: { type: String, default: '歡迎來到vue3' } },
setup(props) {
const router = useRouter();
let realTime = reactive({ count: 0 });
function handleclick() {
router.push({ path: '/other' })
}
return {
msg: props.msg,
realTime,
handleclick
}
}
})
</script>
router
在 src 下新建 router 文件夾,并在文件夾內(nèi)創(chuàng)建 index.ts
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> = [
{
path: '/',
component: () => import("/@/views/index.vue")
},
]
export default createRouter({
history: createWebHistory(),
routes
})
main.ts
把原本的main.js改為main.ts,修改后別忘記把index.html里面也改為main.ts
import { createApp } from 'vue'
import router from './router/index'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css'
import './reset.css'
const app = createApp(App);
app.use(ElementPlus);
app.use(router);
app.mount('#app');
細心的同學這時可能已經(jīng)發(fā)現(xiàn):在 ts 文件中引入 .vue 文件時出現(xiàn)以下報錯,但是不影響代碼正常運行

報錯原因:typescript 只能理解 .ts 文件,無法理解 .vue文件
解決方法:在項目根目錄或 src 文件夾下創(chuàng)建一個后綴為 .d.ts 的文件,并寫入以下內(nèi)容:
declare module '*.vue' { }
declare module '*.js'
declare module '*.json';
declare module '*.svg'
declare module '*.png'
declare module '*.jpg'
declare module '*.jpeg'
declare module '*.gif'
declare module '*.bmp'
declare module '*.tiff'
至此項目搭建完成,可以愉快的寫代碼了。
到此這篇關于vite+vue3.0+ts+element-plus快速搭建項目的實現(xiàn)的文章就介紹到這了,更多相關vite+vue3.0+ts+element-plus搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一文解決vue2 element el-table自適應高度問題
在寫公司后臺項目的時候遇到一個需求,要求表格頁面不能有滾動條,所以必須封裝一個公共方法來實現(xiàn)表格自適應高度,本問小編給大家介紹了如何解決vue2 element el-table自適應高度問題,需要的朋友可以參考下2023-11-11
element-ui table span-method(行合并)的實現(xiàn)代碼
element-ui官網(wǎng)中關于行合并的例子是根據(jù)行號進行合并的,這顯然不符合我們?nèi)粘i_發(fā)需求,因為通常我們table中的數(shù)據(jù)都是動態(tài)生成的,非常具有實用價值,需要的朋友可以參考下2018-12-12
vue計算屬性computed--getter和setter用法
這篇文章主要介紹了vue計算屬性computed--getter和setter用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

