Vue3 中的 Vue-Router 和 VueX詳解
Vue3 中的 Vue-Router 和 VueX
先使用 vue create 指令來(lái)創(chuàng)建 vue 工程項(xiàng)目,并選擇自定義,將 Router 和 VueX 勾上。

勾上以后看主入口 main.js,可以看到項(xiàng)目自動(dòng)幫我們注冊(cè)了 vue-router 和 VueX。
// main.js
createApp(App).use(store).use(router).mount('#app')1. Vue-Router 路由的理解和使用
路由是指根據(jù) url 的不同,展示不同的內(nèi)容。
查看 src 文件夾里的 router,這就是來(lái)處理路由的地方。
其中,index.js 有關(guān)鍵的代碼
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
}
]
定義了兩個(gè)路由項(xiàng)。當(dāng)訪問根目錄,就加載 HomeView 這個(gè)組件。如果訪問 /about,則懶加載 AboutView 這個(gè)組件。
import(...)是異步加載路由的方法。因?yàn)槿绻豢跉馊拷M件加載出來(lái),主頁(yè)的加載的性能將會(huì)很低,為了提高組件的加載性能,使用懶加載按需加載,等進(jìn)入 /about 或其他的頁(yè)面再加載響應(yīng)部分的代碼。但是異步加載代碼也有相應(yīng)的問題,就是去其他的頁(yè)面可能會(huì)有卡頓(其實(shí)就是加載的時(shí)間分?jǐn)偭耍?/p>
因此,vue-router 就是根據(jù) url 不同來(lái)顯示不同組件的特性。
現(xiàn)在查看根組件 App.vue,上面有關(guān)鍵的代碼:
<template>
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</nav>
<router-view/>
</template>router-link 是跳轉(zhuǎn)路由的標(biāo)簽,點(diǎn)擊后就會(huì)跳轉(zhuǎn)到相應(yīng)的路由。
router-view 負(fù)責(zé)展示當(dāng)前路由對(duì)應(yīng)的組件內(nèi)容。例如上面的代碼,當(dāng)跳轉(zhuǎn)到根路徑上時(shí),router 會(huì)進(jìn)行搜索,當(dāng)搜索到匹配項(xiàng)時(shí),便顯示相應(yīng)的組件 HomeView。
2. VueX 語(yǔ)法詳解
在文首就看到了,注冊(cè)了 VueX,接下來(lái)看 src 的 store 目錄,這里邊的 index.js 可以看到以下內(nèi)容,稍后會(huì)講。
export default createStore({
state: {
},
getters: {
},
mutations: {
},
actions: {
},
modules: {
}
})2.1 VueX 是什么
VueX 是數(shù)據(jù)管理框架。之前的數(shù)據(jù)傳遞都是父子之間的傳遞,雖然也有 provider 之類的方法進(jìn)行跨級(jí)傳遞,但是可維護(hù)性也不會(huì)特別高。
VueX 提供了一個(gè)全局都可以使用的數(shù)據(jù)管理倉(cāng)庫(kù),不用考慮父子傳值之類的問題,并且可以跨頁(yè)面?zhèn)鬟f數(shù)據(jù)。提高了可維護(hù)性。
使用方法
提供數(shù)據(jù):在 store 里的 state 里定義一些數(shù)據(jù)
// VueX 創(chuàng)建了一個(gè)全局唯一的倉(cāng)庫(kù),用來(lái)存放全局的數(shù)據(jù)
export default createStore({
state: {
name: "John",
},
getters: {
},
mutations: {
},
actions: {
},
modules: {
}
})在需要用數(shù)據(jù)的地方通過 this.$store 獲取即可
<template>
<div>{{ myName }}</div>
</template>
<script>
export default {
name: "LoginView",
computed: {
myName() {
return this.$store.state.name;
},
},
};
</script>2.2 VueX 數(shù)據(jù)修改流程
講一下 VueX 的修改規(guī)范,因?yàn)槿值臄?shù)據(jù)是不能隨意更改的,因此 VueX 有一套機(jī)制流程,并不能直接用賦值的方法進(jìn)行修改。
修改流程
在組件里提交一個(gè) action 到 store,dispatch 的第一個(gè)參數(shù)是 action 名稱,后面可自定義參數(shù)。
// LoginView.vue
<template>
<div>{{ myName }}</div>
<button @click="handleClick">click</button>
</template>
<script>
export default {
name: "LoginView",
computed: {
myName() {
return this.$store.state.name;
},
},
methods: {
handleClick() {
// 1. 想改變數(shù)據(jù),VueX 要求第一步,必須派發(fā)一個(gè) action,action 名稱為 actions 里的方法名
this.$store.dispatch("change");
},
},
};
</script>store 感知到 action,執(zhí)行 actions 下面對(duì)應(yīng)的方法
actions 對(duì)應(yīng)的方法 commit 信息后傳遞給 mutations。commit 的信息為 mutations 里的方法名。
mutations 里對(duì)應(yīng)的方法執(zhí)行修改數(shù)據(jù)的操作。
import { createStore } from "vuex";
// VueX 創(chuàng)建了一個(gè)全局唯一的倉(cāng)庫(kù),用來(lái)存放全局的數(shù)據(jù)
export default createStore({
state: {
name: "John",
},
getters: {
},
mutations: {
// 4. 接收到 commit 信息后,觸發(fā)對(duì)應(yīng)的 mutation
change(state) {
// 5. 在 mutation 里修改數(shù)據(jù)
state.name = "Modified Name";
},
},
actions: {
// 2. 接收到 action 后執(zhí)行相應(yīng)的方法
change(store) {
// 3. commit 后發(fā)送給 mutations
store.commit("change");
},
},
modules: {},
});從上面的流程來(lái)看,VueX 創(chuàng)建了一個(gè)全局唯一的倉(cāng)庫(kù),用來(lái)存放全局的數(shù)據(jù),同時(shí)里面設(shè)置了一系列的數(shù)據(jù)操作流程。
這流程下來(lái)有點(diǎn)麻煩啊,為啥不直接 commit 過去呢?同步代碼看起來(lái)是如此,但是如果有異步操作的話,這些步驟就很有必要了。
注意mutations 和 actions 里的方法第一個(gè)參數(shù)各不相同。
actions 里面第一個(gè)參數(shù)是 store,因?yàn)樾枰?store.commit 方法來(lái)提交給 mutations。
mutations 里邊的方法第一個(gè)參數(shù)是 state,因?yàn)?store 是用來(lái)修改 store 里 state 的方法。
可見,vue 官方在代碼層面都是推薦分離的。
其他細(xì)則 mutations 里面只允許寫同步代碼,不許寫異步代碼actions 里面允許寫異步操作
優(yōu)點(diǎn)在于,將兩種功能分離,mutations 里做數(shù)據(jù)的改變,actions 里做主要的邏輯書寫,維護(hù)的時(shí)候會(huì)更加方便。
getter 是啥
store 里的數(shù)據(jù)有時(shí)候并不能直接拿來(lái)用,還需要經(jīng)過一些小處理。一般就會(huì)想,在組件內(nèi)處理不就完事了?但是呢,萬(wàn)一有很多的組件都有著需求呢,挨個(gè)寫就不合適。getter 就相當(dāng)于 store 里的 computed 屬性,對(duì) state 進(jìn)行了一定的處理。
使用方法:
設(shè)置 getter
getter 接收兩個(gè)參數(shù),然后返回一個(gè)值。
state,必選,是 store 里的數(shù)據(jù)getters,可選,是 store 里的所有 getters
例如,我要計(jì)算得到大寫后的 name:
// store/index.js
getters: {
upperCasedName: (state) => {
return state.name.toUpperCase();
}
},組件獲取 store 里的 getter
<template>
<div>{{ upperCasedName }}</div>
<button @click="handleClick">click</button>
</template>
<script>
export default {
name: "LoginView",
computed: {
upperCasedName() {
return this.$store.getters.upperCasedName;
},
},
methods: {
handleClick() {
// 1. 想改變數(shù)據(jù),VueX 要求第一步,必須派發(fā)一個(gè) action
this.$store.dispatch("change");
},
},
};
</script>通過 computed 獲取到 getter 后,點(diǎn)擊按鈕改變了 store 里的 name,上邊顯示的仍然是大寫的,因?yàn)楂@取的是全部字母大寫后的 name。
2.3 Composition API 使用 VueX
composition API 使用 useStore 獲取 store 即可。
<template>
<div>{{ name }}</div>
</template>
<script>
import { useStore } from "vuex";
export default {
name: "LoginView",
setup() {
const store = useStore();
const name = store.state.name;
return {
name,
};
},
};
</script>用 toRefs 解構(gòu)的方法也行:
<template>
<div>{{ name }}</div>
<button @click="handleClick">Click</button>
</template>
<script>
import { toRefs } from "@vue/reactivity";
import { useStore } from "vuex";
export default {
name: "LoginView",
setup() {
const store = useStore();
const { name } = toRefs(store.state);
const handleClick = () => {
store.commit("changeName");
};
return {
name,
handleClick,
};
},
};
</script>到此這篇關(guān)于Vue3 中的 Vue-Router 和 VueX的文章就介紹到這了,更多相關(guān)Vue3 中的 Vue-Router 和 VueX內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中定義全局變量、函數(shù)的幾種方法
這篇文章主要介紹了vue項(xiàng)目中定義全局變量、函數(shù)的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue+el-table實(shí)現(xiàn)合并單元格
這篇文章主要為大家詳細(xì)介紹了vue+el-table實(shí)現(xiàn)合并單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue數(shù)據(jù)初始化initState的實(shí)例詳解
Vue 實(shí)例在建立的時(shí)候會(huì)運(yùn)行一系列的初始化操作,而在這些初始化操作里面,和數(shù)據(jù)綁定關(guān)聯(lián)最大的是 initState。這篇文章主要介紹了vue數(shù)據(jù)初始化--initState,需要的朋友可以參考下2019-04-04
Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法淺析
這篇文章主要介紹了Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

