Vue3: 如何以 Vite 建立,以 Vue Router, Vuex, Ant Design 開始應用
- 本文程式碼: https://github.com/ikuokuo/start-vue3
- 線上演示: https://ikuokuo.github.io/start-vue3/ ?
Vite 建立 Vue 3 專案
yarn create vite-app my-site
cd my-site
yarn
yarn dev
執行輸出:
❯ yarn dev
yarn run v1.22.10
warning package.json: No license field
$ vite
vite v1.0.0-rc.4
[vite] Optimizable dependencies detected:
vue
Dev server running at:
> Local: http://localhost:3000/
> Network: http://192.168.1.100:3000/
訪問網址:
與 Vue 2 的差異
詳見: Migration Guide
Vite 配置
vite.config.ts
:
import { resolve } from "path";
function pathResolve(dir: string) {
return resolve(__dirname, ".", dir);
}
module.exports = {
alias: {
"/@/": pathResolve("src"),
},
optimizeDeps: {
include: ["@ant-design/icons-vue"],
},
};
前提準備
eslint-plugin-vue
yarn add -D eslint eslint-plugin-vue
.eslintrc.js
:
module.exports = {
extends: [
// add more generic rulesets here, such as:
// "eslint:recommended",
"plugin:vue/vue3-recommended",
// "plugin:vue/recommended" // Use this if you are using Vue.js 2.x.
],
rules: {
// override/add rules settings here, such as:
"vue/no-multiple-template-root": "off",
},
};
TypeScript
yarn add -D typescript
詳見:
Vue Router
yarn add vue-router@next
Vuex
yarn add vuex@@next
Ant Design Vue
yarn add ant-design-vue@next
# import on demand
yarn add -D babel-plugin-import
# https://github.com/vueComponent/ant-design-vue/issues/2798
yarn add @ant-design/colors
.babelrc
:
{
"plugins": [
["import", { "libraryName": "ant-design-vue", "libraryDirectory": "es", "style": "css" }] // `style: true` 會載入 less 檔案
]
}
其他依賴
yarn add -D sass
開始使用
使用 TypeScript
-
main.js
重新命名為main.ts
-
index.html
裡把/src/main.js
替換為/src/main.ts
... <body> <div id="app"></div> <script type="module" src="/src/main.ts"></script> </body> </html>
使用 Vue Router
router/index.ts
:
import { createRouter, createWebHistory } from "vue-router";
const routes = [
{
path: "/",
name: "Home",
component: () => import("/@/views/Home.vue"),
},
{
path: "/setting",
name: "Setting",
component: () => import("/@/views/Setting.vue"),
},
];
export default createRouter({
history: createWebHistory(),
routes,
});
使用 Vuex
store/index.ts
:
import { createStore } from "vuex";
export default createStore({
state() {
return {
count: 0,
};
},
mutations: {
increment(state) {
state.count++;
},
},
actions: {
increment(context) {
context.commit("increment");
},
},
});
新增 Views
views/Home.vue
:
<template>
<h1>This is a home page</h1>
<HelloWorld msg="Hello Vue 3.0 + Vite" />
</template>
<script lang="ts">
import { defineComponent } from "vue";
import HelloWorld from "/@/components/HelloWorld.vue";
export default defineComponent({
name: "Home",
components: {
HelloWorld,
},
});
</script>
views/Setting.vue
:
<template>
<div>
<h1>This is a setting page</h1>
<p>store count is: {{ count }}</p>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "Setting",
computed: {
count() {
return this.$store.state.count;
},
},
});
</script>
更改 main.ts
應用 Vue Router, Vuex, Ant Design :
import { createApp } from "vue";
import router from "/@/router";
import store from "/@/store";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
import App from "/@/App.vue";
import "/@/styles/index.scss";
const app = createApp(App);
app.use(router);
app.use(store);
app.use(Antd);
app.mount("#app");
更改 App.vue
使用 Ant Design 搭建首頁佈局,其中路由部分如下:
<a-layout-header style="background: #fff; padding: 0">
<a-row type="flex" justify="end">
<a-col class="mr-3">
<a-button
type="primary"
shape="circle"
size="large"
@click="this.$router.push('/')"
>
<template #icon><HomeFilled /></template>
</a-button>
</a-col>
<a-col class="mr-3">
<a-button
type="primary"
shape="circle"
size="large"
@click="this.$router.push('/setting')"
>
<template #icon><SettingFilled /></template>
</a-button>
</a-col>
</a-row>
</a-layout-header>
<a-layout-content style="margin: 0 16px">
<div :style="{ padding: '24px', background: '#fff', minHeight: '360px' }">
<router-view />
</div>
</a-layout-content>
釋出到 GitHub Pages
編輯 vite.config.ts
新增 base
路徑:
module.exports = {
// otherwise, may assets 404 or visit with index.html
base: "/start-vue3/",
assetsDir: "",
};
編輯 router/index.ts
新增 base
路徑:
export default createRouter({
history: createWebHistory("/start-vue3/"),
routes,
});
編譯,再發布:
cd my-site
yarn build
export GIT_HASH=`git rev-parse --short HEAD`
cd dist/
git init
git remote add origin git@github-ik:ikuokuo/start-vue3.git
git checkout --orphan gh-pages
git add .
git commit -m "deploy website - based on $GIT_HASH"
git push origin gh-pages
訪問 https://ikuokuo.github.io/start-vue3/ 體驗線上演示。
參考
結語
歡迎關注 GoCoding 公眾號,分享日常 Coding 中實用的小技巧、小知識!
相關文章
- Electron: 如何以 Vue.js, Vuetify 開始應用Vue.js
- yarn + vue3 + vite 建立專案YarnVueVite
- 用vite建立vue3專案報錯解決ViteVue
- Ant Design VueVue
- 全網最硬核的Ant-Design-Vue從Vue-cli遷移至ViteVueVite
- ant-design-vueVue
- 《Asp.Net Core3 + Vue3入坑教程》 - Vue 2.使用Ant Design of Vue編寫頁面 & vue-router 初試ASP.NETVue
- Vite + Vue3 初體驗 —— Vite 篇ViteVue
- Vite + Vue3 初體驗 —— Vue3 篇ViteVue
- vite + vue3 + typescript 搭建ViteVueTypeScript
- vite搭建vue專案-整合別名@、router、vuex、scss就是這樣簡單ViteVueCSS
- Vue3+Vite2.6+TypeScript+Ant-design-vue構建企業級中後臺響應式管理後臺VueViteTypeScript
- Vue3實戰系列:結合 Ant-Design-of-Vue 實踐 Composition APIVueAPI
- vue+vue-router+vuex地址管理思路Vue
- 怎麼知道 vite 建立的 vue 專案是 vue2還是vue3?ViteVue
- Vue+Vue-router+Vuex專案實戰Vue
- 3 vite vue-router 路由巢狀ViteVue路由巢狀
- Vue3 + Vite2 + TypeScript + Pinia(Vuex)+JSX 搭建企業級開發腳手架【開箱即用】VueViteTypeScriptJS
- Ant Design Vue專案解析-前言Vue
- Vue+Ant Design實現CRUDVue
- 在Vue3中使用vuexVue
- 從頭開始學習vue-routerVue
- 解密vue-router: 從原始碼開始解密Vue原始碼
- vue專案實踐(vuex + vue-router + vue-resource)Vue
- vue使用ant design vue(upload)檔案上傳Vue
- vue3+Ant Design Vue整合MarkDone編輯框Vue
- vue3 vite node18 專案搭建VueVite
- Ant Design Vue 的 table 隱藏特定列Vue
- Ant Design Vue 的 slots 是幹嘛的?Vue
- ant-design-vue元件庫原始碼分析Vue元件原始碼
- Element Plus 和 Ant Design Vue哪個好Vue
- Vue3.0+typescript+Vite+Pinia+Element-plus搭建vue3框架!VueTypeScriptVite框架
- 搞一下vue生態,從vuex開始Vue
- Ant Design 官方《Ant Design 實戰教程》釋出
- Vite + Vue3 動態路由 - 踩坑記錄ViteVue路由
- vite中配置less,vue3中配置lessViteVue
- vite如何打包vue3外掛為JSSDKViteVueJS
- Vue3.0+vue-router-next+vuex4.0+typescript專案搭建VueTypeScript