成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用Vite2+TypeScript4+Vue3技術(shù)棧,如何入手開發(fā)項(xiàng)目

開發(fā) 項(xiàng)目管理
已經(jīng)兩周沒有發(fā)文了,自己臨時(shí)有點(diǎn)事耽誤了,在這里向大家表示深深地歉意。今天,我們使用vite2.0+vue3+ts來開發(fā)一個(gè)demo項(xiàng)目。

[[388812]]

前言

已經(jīng)兩周沒有發(fā)文了,自己臨時(shí)有點(diǎn)事耽誤了,在這里向大家表示深深地歉意。今天,我們使用vite2.0+vue3+ts來開發(fā)一個(gè)demo項(xiàng)目。

實(shí)戰(zhàn)

我們,打開Vite官方網(wǎng)站(https://cn.vitejs.dev/)。

  • Vite (法語意為 "快速的",發(fā)音 /vit/) 是一種新型前端構(gòu)建工具,能夠顯著提升前端開發(fā)體驗(yàn)。它主要由兩部分組成:
  • 一個(gè)開發(fā)服務(wù)器,它基于 原生 ES 模塊 提供了 豐富的內(nèi)建功能,如速度快到驚人的 模塊熱更新(HMR)。
  • 一套構(gòu)建指令,它使用 Rollup 打包你的代碼,并且它是預(yù)配置的,可以輸出用于生產(chǎn)環(huán)境的優(yōu)化過的靜態(tài)資源。
  • Vite 意在提供開箱即用的配置,同時(shí)它的 插件 API 和 JavaScript API 帶來了高度的可擴(kuò)展性,并有完整的類型支持。

這里,我們將Vite與VueCLI做一下對(duì)比。

  • Vite在開發(fā)模式下不需要打包可以直接運(yùn)行,使用的是ES6的模塊化加載規(guī)則;
  • VueCLI開發(fā)模式下必須對(duì)項(xiàng)目打包才可以運(yùn)行;
  • Vite基于緩存的熱更新;
  • VueCLI基于webpack的熱更新;

搭建項(xiàng)目

我們來搭建第一個(gè) Vite 項(xiàng)目,我這里使用Yarn依賴管理工具進(jìn)行創(chuàng)建項(xiàng)目。

  1. yarn create @vitejs/app 

在命令行鍵入以上命令,然后你可能會(huì)等待一會(huì)兒~

依次配置Project name、Select a template

  1. Project name: vite-vue-demo 
  2.  
  3. Select a template: vue-ts 

因?yàn)椋覀冞@里要是用Vue+Ts開發(fā)項(xiàng)目所以我們選擇vue-ts這個(gè)模板。一頓操作之后,會(huì)提示你鍵入以下命令,依次填入即可。

  1. cd vite-vue-demo 
  2. yarn 
  3. yarn dev 

這樣我們搭建項(xiàng)目就完成了。

項(xiàng)目文件夾一覽

我們會(huì)看到以下文件及其文件夾。

  1. node_modules  ---依賴文件夾 
  2. public  ---公共文件夾 
  3. src  ---項(xiàng)目主要文件夾 
  4. .gitignore  ---排除git提交配置文件 
  5. index.html  ---入口文件 
  6. package.json  ---模塊描述文件 
  7. tsconfig.json  ---ts配置文件 
  8. vite.config.ts  ---vite配置文件 

開發(fā)前配置

在開發(fā)之前呢,我們需要做以下工作。

1. 編輯ts配置文件

根據(jù)需要,配置需要的配置項(xiàng)。compilerOptions里面配置的是編譯時(shí)的配置項(xiàng),include項(xiàng)是配置生效包括在內(nèi)的路徑,而exclude則恰恰相反,排除在外的路徑。

  1.   "compilerOptions": { 
  2.     "target""esnext"
  3.     "module""esnext"
  4.     "strict"true
  5.     "jsx""preserve"
  6.     "importHelpers"true
  7.     "moduleResolution""node"
  8.     "experimentalDecorators"true
  9.     "skipLibCheck"true
  10.     "esModuleInterop"true
  11.     "allowSyntheticDefaultImports"true
  12.     "sourceMap"true
  13.     "baseUrl""."
  14.     "types": ["vite/client"], 
  15.     "paths": { 
  16.       "@/*": [ 
  17.         "src/*" 
  18.       ] 
  19.     }, 
  20.     "lib": [ 
  21.       "esnext"
  22.       "dom"
  23.       "dom.iterable"
  24.       "scripthost" 
  25.     ] 
  26.   }, 
  27.   "include": [ 
  28.     "src/**/*.ts"
  29.     "src/**/*.tsx"
  30.     "src/**/*.vue"
  31.     "tests/**/*.ts"
  32.     "tests/**/*.tsx" 
  33.   ], 
  34.   "exclude": [ 
  35.     "node_modules" 
  36.   ] 

2. 配置vite配置文件

為什么需要配置這個(gè)文件呢?是因?yàn)槲覀冮_發(fā)這個(gè)demo項(xiàng)目,需要局部引入Element Plus UI框架,另外,讓我們簡(jiǎn)單了解下怎么配置Vite。在之前我們使用VueCLI3.x創(chuàng)建項(xiàng)目時(shí)配置項(xiàng)目是在根目錄下vue.config.js文件下進(jìn)行配置。這個(gè)文件應(yīng)該導(dǎo)出一個(gè)包含了選項(xiàng)的對(duì)象。

  1. module.exports = { 
  2.   // 選項(xiàng)... 

而vite.config.ts也與其相似。

  1. export default { 
  2.   // 配置選項(xiàng) 

因?yàn)?Vite 本身附帶 Typescript 類型,所以可以通過 IDE 和 jsdoc 的配合來進(jìn)行智能提示,另外你可以使用 defineConfig 幫手函數(shù),這樣不用 jsdoc 注解也可以獲取類型提示。這里呢,我們這樣配置vite.config.ts。

  1. import { defineConfig } from 'vite' 
  2. import vue from '@vitejs/plugin-vue' 
  3.  
  4. // https://vitejs.dev/config/ 
  5. export default defineConfig({ 
  6.   plugins: [vue()] 
  7. }) 

你會(huì)發(fā)現(xiàn),Vue在這里被當(dāng)做vite的一個(gè)內(nèi)置插件引入進(jìn)來。剛才,我們說要局部引入Element Plus UI框架,我們打開Element Plus UI局部引入網(wǎng)址:(https://element-plus.gitee.io/#/zh-CN/component/quickstart),發(fā)現(xiàn)這里需要配置babel.config.js,而我們使用的是TS,所以我們下意識(shí)的更改下后綴名覺得就可以成功了,不過,我反正被坑了。于是,采取了這種辦法:在vite.config.ts文件中這樣配置:

  1. import { defineConfig } from 'vite' 
  2. import vue from '@vitejs/plugin-vue' 
  3. import vitePluginImp from "vite-plugin-imp"
  4.  
  5. // https://vitejs.dev/config/ 
  6. export default defineConfig({ 
  7.   plugins: [vue(), 
  8.     vitePluginImp({ 
  9.     libList: [ 
  10.       { 
  11.         libName: 'element-plus'
  12.         style: (name) => { 
  13.           return`element-plus/lib/theme-chalk/${name}.css` 
  14.         } 
  15.       } 
  16.     ] 
  17.   })], 
  18.   server: { 
  19.     port: 6061 
  20.   }, 
  21. }) 

vite-plugin-imp一個(gè)自動(dòng)導(dǎo)入組件庫樣式的vite插件。

主要項(xiàng)目文件夾Src一覽

以下是最初始的項(xiàng)目文件目錄。

  1. assets  ---靜態(tài)文件夾 
  2. components  ---組件文件夾 
  3. App.vue  ---頁面文件 
  4. main.ts  ---項(xiàng)目入口文件 
  5. shims-vue.d.ts  ---類型定義文件(描述文件) 

這么多文件,我們不一一展示了,主要看下App.vue、main.ts、shims-vue.d.ts。

App.vue

  1. <template> 
  2.   <img alt="Vue logo" src="./assets/logo.png" /> 
  3.   <HelloWorld msg="Hello Vue 3 + TypeScript + Vite" /> 
  4. </template> 
  5.  
  6. <script lang="ts"
  7. import { defineComponent } from 'vue' 
  8. import HelloWorld from './components/HelloWorld.vue' 
  9.  
  10. export default defineComponent({ 
  11.   name'App'
  12.   components: { 
  13.     HelloWorld 
  14.   } 
  15. }) 
  16. </script> 
  17.  
  18. <style> 
  19. #app { 
  20.   font-family: Avenir, Helvetica, Arial, sans-serif; 
  21.   -webkit-font-smoothing: antialiased; 
  22.   -moz-osx-font-smoothing: grayscale; 
  23.   text-align: center; 
  24.   color: #2c3e50; 
  25.   margin-top: 60px; 
  26. </style> 

main.ts

  1. import { createApp } from 'vue' 
  2. import App from './App.vue' 
  3.  
  4. createApp(App).mount('#app'

shims-vue.d.ts

  1. declare module '*.vue' { 
  2.   import { DefineComponent } from 'vue' 
  3.   const component: DefineComponent<{}, {}, any
  4.   export default component 

這里,我們看到defineComponent這個(gè)Vue3的一個(gè)方法。為什么這里會(huì)使用它呢?引用官方的一句話:

  • 從實(shí)現(xiàn)上看,defineComponent 只返回傳遞給它的對(duì)象。但是,就類型而言,返回的值有一個(gè)合成類型的構(gòu)造函數(shù),用于手動(dòng)渲染函數(shù)、TSX 和 IDE 工具支持。

引入vue-router4

看完之前的基礎(chǔ)配置,我們現(xiàn)在準(zhǔn)備開始引入Vue3的生態(tài)系統(tǒng)。

現(xiàn)在我們安裝 vue-router 版本的時(shí)候,默認(rèn)還是安裝的 3.x 版本的,由于 vue3 的更新發(fā)生很大的變化,所以為了兼容處理,vue-router 也將發(fā)布最新版 4.x 版本了。

這是router4的官方網(wǎng)址:

  1. https://next.router.vuejs.org/ 

1. 安裝

  1. npm install vue-router@4 

2. 配置文件

安裝完依賴后,在項(xiàng)目文件夾src下創(chuàng)建一個(gè)router文件夾,里面創(chuàng)建一個(gè)index.ts文件(因?yàn)檫@里我們基于TS的項(xiàng)目)。

  1. import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"
  2. import Home from "../views/Home.vue"
  3.  
  4. const routes: Array<RouteRecordRaw> = [ 
  5.     { 
  6.         path: "/"
  7.         name"Home"
  8.         component: Home 
  9.     }, 
  10.     { 
  11.         path: "/about"
  12.         name"About"
  13.         component: () => 
  14.             import("../views/About.vue"
  15.     } 
  16. ]; 
  17.  
  18. const router = createRouter({ 
  19.     history: createWebHashHistory(), 
  20.     routes 
  21. }); 
  22.  
  23. export default router; 

另外,你需要在main.ts文件中引入它,并且注冊(cè)一下。

  1. import { createApp } from "vue"
  2. import App from "./App.vue"
  3. import router from "./router"
  4.  
  5. createApp(App).use(router).mount("#app"); 

為了實(shí)驗(yàn)一下效果,我們?cè)趕rc文件夾下創(chuàng)建一個(gè)views文件夾,里面創(chuàng)建兩個(gè)頁面文件。分別是About.vue和Home.vue。

home.vue

  1. <template> 
  2.   <p class="txt">home</p> 
  3. </template> 
  4.  
  5. <script lang="ts"
  6. import { Options, Vue } from "vue-class-component"
  7.  
  8. @Options({ 
  9.  
  10. }) 
  11. export default class Home extends Vue {} 
  12. </script> 
  13.  
  14. <style scoped> 
  15. .txt{ 
  16.   color: red; 
  17. </style> 

about.vue

  1. <template> 
  2.   <p>about</p> 
  3. </template> 
  4.  
  5. <script> 
  6. export default { 
  7. name"About" 
  8. </script> 

最后,你在App.vue文件中。

  1. <template> 
  2.   <router-link to="/">Home</router-link> | 
  3.   <router-link to="/about">About</router-link> 
  4.   <router-view /> 
  5. </template> 
  6.  
  7. <script lang="ts"
  8. </script> 

這樣,vue-router4就這么成功引入了。

引入css預(yù)處理語言

這里呢,我們引入scss。因?yàn)槲覀兪褂玫膙ite這個(gè)構(gòu)建工具構(gòu)建的項(xiàng)目,所以我們只需要這樣。

  1. npm install -D sass 

我們可以看到官方解釋:

  • Vite 同時(shí)提供了對(duì) .scss, .sass, .less, .styl 和 .stylus 文件的內(nèi)置支持。沒有必要為他們安裝特定的 vite 插件,但相應(yīng)的預(yù)處理器依賴本身必須安裝。

所以,你可以這樣使用scss。

  1. <template> 
  2.   <p class="txt">home</p> 
  3. </template> 
  4.  
  5. <script lang="ts"
  6. import { Options, Vue } from "vue-class-component"
  7.  
  8. @Options({ 
  9.  
  10. }) 
  11. export default class Home extends Vue {} 
  12. </script> 
  13.  
  14. <style scoped lang="scss"
  15. .txt{ 
  16.   color: red; 
  17. </style> 

使用Element Plus UI

我們?cè)谏厦嬉呀?jīng)成功配置局部引入Element Plus框架,那我們可以這樣使用它。

  1. <template> 
  2.   <p class="txt">home</p> 
  3.   <ElButton>默認(rèn)按鈕</ElButton> 
  4. </template> 
  5.  
  6. <script lang="ts"
  7. import { Options, Vue } from "vue-class-component"
  8. import { ElButton } from 'element-plus' 
  9.  
  10. @Options({ 
  11.   components: { 
  12.     ElButton 
  13.   } 
  14. }) 
  15. export default class Home extends Vue {} 
  16. </script> 
  17.  
  18. <style scoped lang="scss"
  19. .txt{ 
  20.   color: red; 
  21. </style> 

這里,你會(huì)發(fā)現(xiàn)引入了 vue-class-component這個(gè)組件,它是干什么的呢?

官方網(wǎng)址:

  1. https://class-component.vuejs.org/ 
  • Vue Class Component is a library that lets you make your Vue components in class-style syntax.

譯:Vue類組件是一個(gè)庫,允許您以類樣式語法創(chuàng)建Vue組件。

針對(duì)vue3版本,我們使用Vue Class Component v8,也就是8版本。

  1. https://www.npmjs.com/package/vue-class-component/v/8.0.0-rc.1 

你可以這樣安裝它。

  1. npm i vue-class-component@8.0.0-rc.1 

引入vue自定義組件

我們經(jīng)常自己封裝組件,那么在這個(gè)項(xiàng)目中我們是怎樣引入的呢?我們?cè)趕rc目錄下創(chuàng)建一個(gè)components文件夾,里面創(chuàng)建一個(gè)HelloWorld.vue文件。

HelloWorld.vue

  1. <template> 
  2.   <h1>{{ msg }}</h1> 
  3. </template> 
  4.  
  5. <script lang="ts"
  6. import { ref, defineComponent } from 'vue' 
  7. export default defineComponent({ 
  8.   name'HelloWorld'
  9.   props: { 
  10.     msg: { 
  11.       type: String, 
  12.       required: true 
  13.     } 
  14.   }, 
  15.   setup: () => { 
  16.     const count = ref(0) 
  17.     return { count } 
  18.   } 
  19. }) 
  20. </script> 
  21.  
  22. <style scoped lang="scss"
  23. a { 
  24.   color: #42b983; 
  25.  
  26. label { 
  27.   margin: 0 0.5em; 
  28.   font-weight: bold; 
  29.  
  30. code { 
  31.   background-color: #eee; 
  32.   padding: 2px 4px; 
  33.   border-radius: 4px; 
  34.   color: #304455; 
  35. </style> 

然后,我們?cè)贏pp.vue引入它。

  1. <template> 
  2.   <HelloWorld msg="Hello Vue 3 + TypeScript + Vite" /> 
  3.   <router-link to="/">Home</router-link> | 
  4.   <router-link to="/about">About</router-link> 
  5.   <router-view /> 
  6. </template> 
  7.  
  8. <script lang="ts"
  9. import { defineComponent } from 'vue' 
  10. import HelloWorld from './components/HelloWorld.vue' 
  11.  
  12. export default defineComponent({ 
  13.   name'App'
  14.   components: { 
  15.     HelloWorld 
  16.   } 
  17. }) 
  18. </script> 
  19.  
  20. <style > 
  21. #app { 
  22.   font-family: Avenir, Helvetica, Arial, sans-serif; 
  23.   -webkit-font-smoothing: antialiased; 
  24.   -moz-osx-font-smoothing: grayscale; 
  25.   text-align: center; 
  26.   color: #2c3e50; 
  27. </style> 

引入vuex4

vue生態(tài)中肯定少不了vuex,為了兼容vue3,vuex也推出了4.0版本。

  1. https://next.vuex.vuejs.org/ 

你可以這樣安裝它。

  1. npm install vuex@next --save 

你可以在src文件夾創(chuàng)建一個(gè)store文件夾,里面創(chuàng)建一個(gè)一個(gè)index.ts文件。

  1. import { createStore } from "vuex"
  2.  
  3. export default createStore({ 
  4.     state: {}, 
  5.     mutations: {}, 
  6.     actions: {}, 
  7.     modules: {} 
  8. }); 

然后,你在main.ts文件中這樣引入使用它。

  1. import { createApp } from "vue"
  2. import App from "./App.vue"
  3. import router from "./router"
  4. import store from "./store"
  5.  
  6. createApp(App).use(router).use(store) 
  7.     .mount("#app"); 

結(jié)語

我們這里只是簡(jiǎn)單地使用vite+ts+vue3搭建了一個(gè)小demo,如果你想更深層地使用它,可以關(guān)注我的動(dòng)態(tài)。

 

責(zé)任編輯:姜華 來源: 前端歷劫之路
相關(guān)推薦

2021-09-26 00:24:58

開發(fā)項(xiàng)目TypeScript

2024-10-18 10:49:03

Actions異步函數(shù)

2022-05-17 08:39:05

VueViteTypeScript

2022-08-15 07:34:36

vite項(xiàng)目Vue3

2021-07-06 07:02:41

Vue 2 Vite 開發(fā)工具

2024-11-06 10:16:22

2022-07-27 08:40:06

父子組件VUE3

2022-07-28 08:26:18

Vue3Uni-appVite

2020-10-25 18:43:20

VueTypeScript前端

2023-12-18 09:53:27

系統(tǒng)管理

2023-03-13 07:52:13

2022-12-12 08:56:45

Vite3Vite

2024-12-30 14:40:20

2025-06-27 17:52:59

ViteVue前端

2025-04-09 09:10:00

開發(fā)ViteVue

2022-08-09 10:00:57

ViteTypeScripVue3

2023-10-26 07:37:18

ReactVue項(xiàng)目

2017-07-26 13:51:19

前端JavaScriptTypeScript

2017-08-07 18:45:51

前端JavaScript技術(shù)棧

2021-05-26 10:40:28

Vue3TypeScript前端
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产在线a| 午夜丰满少妇一级毛片 | 99影视 | 日韩欧美久久精品 | 国产高清一区二区 | 久久久久免费观看 | 免费在线视频一区二区 | 亚洲另类春色偷拍在线观看 | 久久综合一区 | 欧美一区二区成人 | 色毛片 | 福利av在线 | av网站免费 | 欧美亚洲一区二区三区 | 欧美日韩在线观看一区二区三区 | 久久乐国产精品 | 国产精品资源在线观看 | 久久成人国产精品 | 久久精品亚洲 | www.色.com| 成人a视频在线观看 | 国产高清精品一区二区三区 | 成人免费在线观看 | 亚洲一区免费 | 男女国产视频 | 水蜜桃久久夜色精品一区 | 在线日韩福利 | 天天躁日日躁狠狠很躁 | 精品国产免费一区二区三区演员表 | 国产精品夜间视频香蕉 | 久久成人一区二区三区 | 欧美精品二区三区 | 久久人人网 | 一久久久| 一区二区高清 | 在线观看av中文字幕 | 国内激情av片 | 成人av片在线观看 | 亚洲精视频| 欧美精品福利 | 久久99精品国产 |