使用Vite+Vue3+TypeScript?搭建開發(fā)腳手架的詳細(xì)過程
Vite前端開發(fā)與構(gòu)建工具
開發(fā)環(huán)境中,vite無需打包,可快速的冷啟動
真正的按需編譯,不需要等待整個應(yīng)用編譯完成
一個開發(fā)服務(wù)器,它基于原生ES模塊 提供了豐富的內(nèi)建功能,速度快模塊熱更新(HMR)
一套構(gòu)建指令,它使用Rollup打包代碼,并且它是預(yù)配置的,可輸出用于生產(chǎn)環(huán)境的高度優(yōu)化過的靜態(tài)資源。
Vue3 與 Vue2區(qū)別
Vue2 使用 Options API 而 Vue3 使用的 Composition API
TypeScript
在應(yīng)用中對類型判斷的定義和使用有很強(qiáng)的表現(xiàn)。同一對象的多個鍵返回值必須通過定義對應(yīng)的接口(interface)來進(jìn)行類型定義。要不然在 ESLint 時都會報錯
使用Vite創(chuàng)建腳手架
注意:Vite 需要 Node.js 版本 >= 12.0.0
1、創(chuàng)建項(xiàng)目文件夾
例如:想在workSpace文件下創(chuàng)建 my-vue-app,則先進(jìn)入workplace文件夾,再打開cmd,運(yùn)行一下命令
# npm 6.x npm init vite@latest my-vue-app --template vue # npm 7+, 需要額外的雙橫線: npm init vite@latest my-vue-app -- --template vue
2、選擇Vue
3、選擇TypeScript
4、完成后可以看到項(xiàng)目文件夾(my-vue-app)
然后根據(jù)指令,進(jìn)入文件夾,安裝依賴,運(yùn)行項(xiàng)目
完成后效果
配置文件引用別名 alias
修改 vite.config.ts 文件配置(此時:會報錯 path 未定義,接下來定義path)
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path' // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], // 配置文件引用別名 alias resolve: { alias: { '@': path.resolve(__dirname, 'src'), }, }, })
定義path,修改tsconfig.json
{ "compilerOptions": { "target": "ESNext", "useDefineForClassFields": true, "module": "ESNext", "moduleResolution": "Node", "strict": true, "jsx": "preserve", "resolveJsonModule": true, "isolatedModules": true, "esModuleInterop": true, "lib": ["ESNext", "DOM"], "skipLibCheck": true, "noEmit": true, "baseUrl": ".", "paths": { "@/*":["src/*"] // 未設(shè)置 "baseUrl" 時,不允許使用非相對路徑。是否忘記了前導(dǎo) "./",所以添加一個baseUrl } }, "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], "references": [{ "path": "./tsconfig.node.json" }] }
安裝css處理器插件scss
npm install sass-loader sass webpack --save-dev 或yarn add sass-loader --dev npm i dart-sass 或yarn add dart-sass --dev npm i npm-sass 或yarn add sass --dev
配置全局scss樣式(在src/assets 下創(chuàng)建style 文件夾,用于存放全局樣式文件,創(chuàng)建main.scss文件,用于測試)
$test-color: rgb(255, 0, 60);
在組件 HelloWorld.vue文件中 添加測試元素與綁定測試樣式
僅僅這樣會編譯報錯,還需要在vite.config.ts 中增加 全局樣式配置
// 全局配置 樣式變量 css:{ preprocessorOptions:{ scss:{ additionalData:'@import "@/assets/style/main.scss";' } } },
完整配置如下圖
效果圖片
安裝路由 vue-router
npm i vue-router yarn add vue-router@4
在src 文件夾下 創(chuàng)建 router 文件夾 -》并新建router.ts 文件,文件內(nèi)容如下
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'; const routes: RouteRecordRaw[] = [ { path: '/home', name: 'home', component: () => import('@/views/Home/index.vue'), //可能問題1 注意這里要帶上 文件后綴.vue }, { path: '/', name: 'helloWorld', component: () => import('@/components/HelloWorld.vue'), //可能問題1 注意這里要帶上 文件后綴.vue }, { path: '/helloWorld', name: 'helloWorld', component: () => import('@/components/HelloWorld.vue'), //可能問題1 注意這里要帶上 文件后綴.vue }, ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router;
新建頁面:在src 文件夾下創(chuàng)建 views文件夾-》創(chuàng)建 home 文件夾-》創(chuàng)建 index.vue 文件,文件內(nèi)容如下
<template> <h1>這是 home 頁</h1> <router-link :to="{path:'/helloWorld'}">跳轉(zhuǎn)到helloWord(router-link)</router-link> <br/> <button @click="goHelloWordPage">跳轉(zhuǎn)到 helloWord(js_function)</button> </template> <script setup lang="ts"> import {useRouter} from 'vue-router' // 2. 調(diào)用useRouter函數(shù) const $r = useRouter(); const goHelloWordPage = () =>{ $r.push("helloWorld") } </script>
在入口文件main.ts 中 配置路由
import { createApp } from 'vue' import './style.css' import App from './App.vue' import router from '@/router/router' const app = createApp(App) app.use(router) app.mount('#app') //createApp(App).mount('#app')
注意:配置完成后發(fā)現(xiàn) 瀏覽器地址欄:http://127.0.0.1:5174/home 無法跳轉(zhuǎn),需要在App.vue中配置路由容器,原有的template 替換為 一下內(nèi)容
<template> <!-- <h1>這是 主容器</h1> --> <router-view></router-view> </template>
狀態(tài)管理 Pinia
安裝
npm i pinia yarn add pinia@next
在main.ts 中注冊 pinia
// 導(dǎo)入組件 import { createPinia } from "pinia" # 創(chuàng)建根存儲庫并將其傳遞給應(yīng)用程序 app.use(createPinia())
定義狀態(tài):在src文件夾下 創(chuàng)建store文件夾-》創(chuàng)建main.ts文件,文件內(nèi)容如下
import { defineStore } from 'pinia' export const useMainStore = defineStore({ id: 'main', state: () =>({ name: '群主' }), getters: { nameLength: (state) => state.name.length, } })
組件中使用與修改:
<template> <div>這是狀態(tài)管理Pinia:{{ mainStore.name }}<br />長度:{{ mainStore.nameLength }}</div> <button @click="updateName">修改 store 中的 name</button> </template> <script setup lang="ts"> import { useMainStore } from "@/store/main"; const mainStore = useMainStore() const updateName = () => { // $patch 修改 store 中的數(shù)據(jù) mainStore.$patch({ name: "名稱被修改了,nameLength也隨之改變了", }); }; </script>
例如在 home->index.vue 中使用
<template> <h1>這是 home 頁</h1> <router-link :to="{path:'/helloWorld'}">跳轉(zhuǎn)到helloWord(router-link)</router-link> <button @click="goHelloWordPage">跳轉(zhuǎn)到 helloWord(js_function)</button> <div>這是狀態(tài)管理Pinia:{{ mainStore.name }}<br />長度:{{ mainStore.nameLength }}</div> <button @click="updateName">修改 store 中的 name</button> </template> <script setup lang="ts"> import {useRouter} from 'vue-router' import { useMainStore } from "@/store/main"; const mainStore = useMainStore() const updateName = () => { // $patch 修改 store 中的數(shù)據(jù) mainStore.$patch({ name: "名稱被修改了,nameLength也隨之改變了", }); }; // 2. 調(diào)用useRouter函數(shù) const $r = useRouter(); const goHelloWordPage = () =>{ $r.push("helloWorld") } </script>
環(huán)境變量配置
vite提供了開發(fā)模式(development)和生產(chǎn)模式(product)
項(xiàng)目根目錄創(chuàng)建開發(fā)環(huán)境 .enc.dev 文件,文件內(nèi)容如下
NODE_ENV=development VITE_APP_WEB_URL= 'https://www.baidu.com'
項(xiàng)目根目錄創(chuàng)建生產(chǎn)環(huán)境 .enc.prod 文件,文件內(nèi)容如下
NODE_ENV=production VITE_APP_WEB_URL='https://www.goole.com'
使用:在views->home->index.vue 中 添加
<template> <p>當(dāng)前環(huán)境:{{ env.NODE_ENV }}</p> </template> <script setup lang="ts"> const env = import.meta.env </script>
最后修改 pacakge.json 生效
"scripts": { "dev": "vite --mode dev", "dev:prod": "vite --mode prod", "build": "vue-tsc && vite build", "build:dev": "vue-tsc --noEmit && vite build --mode dev", "build:uat": "vue-tsc --noEmit && vite build --mode uat", "build:prod": "vue-tsc --noEmit && vite build --mode prod", "preview": "vite preview" },
啟動項(xiàng)目時:
npm run dev
npm run dev:prod
不同的啟動方式,環(huán)境變量值不同
到此這篇關(guān)于Vite+Vue3+TypeScript 搭建開發(fā)腳手架的文章就介紹到這了,更多相關(guān)Vite+Vue3+TypeScript 搭建腳手架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3結(jié)合typescript中使用class封裝axios
- vue3+vite3+typescript實(shí)現(xiàn)驗(yàn)證碼功能及表單驗(yàn)證效果
- vue?props使用typescript自定義類型的方法實(shí)例
- 基于Vue3+TypeScript的全局對象的注入和使用詳解
- Vue3+TypeScript+Vite使用require動態(tài)引入圖片等靜態(tài)資源
- vue3+Pinia+TypeScript?實(shí)現(xiàn)封裝輪播圖組件
- Vue+TypeScript中處理computed方式
- vue項(xiàng)目中使用ts(typescript)入門教程
- vue3中如何使用vue-types
相關(guān)文章
Vue+ElementUI怎么處理超大表單實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于Vue+ElementUI怎么處理超大表單實(shí)例講解內(nèi)容,以后需要的朋友可以跟著學(xué)習(xí)參考下。2021-11-11詳解Vue實(shí)戰(zhàn)指南之依賴注入(provide/inject)
這篇文章主要介紹了詳解Vue實(shí)戰(zhàn)指南之依賴注入(provide/inject),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11基于vue-simple-uploader封裝文件分片上傳、秒傳及斷點(diǎn)續(xù)傳的全局上傳插件功能
這篇文章主要介紹了基于vue-simple-uploader封裝文件分片上傳、秒傳及斷點(diǎn)續(xù)傳的全局上傳插件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02vue跨域處理方式(vue項(xiàng)目中baseUrl設(shè)置問題)
這篇文章主要介紹了vue跨域處理方式(vue項(xiàng)目中baseUrl設(shè)置問題),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05詳解如何在vue項(xiàng)目中使用layui框架及采坑
這篇文章主要介紹了vue使用layui框架,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05vue實(shí)現(xiàn)動態(tài)路由詳細(xì)
我們開發(fā)后臺管理系統(tǒng)過程中,會由不同的人操作系統(tǒng),有admin(管理員)、superAdmin(超管),及各種運(yùn)營、財(cái)務(wù)人員。為了區(qū)別這些人員,會給不同的人分配不一樣的角色來展示不同的菜單,這就必須要通過動態(tài)路由來實(shí)現(xiàn)。下面就來介紹vue實(shí)現(xiàn)動態(tài)路由,需要的朋友可參考一下2021-10-10在vue中實(shí)現(xiàn)PDF文件流預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹如何在vue中實(shí)現(xiàn)PDF文件流預(yù)覽功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考價值,需要的可以參考一下2023-12-12