Vuex與Vue router的使用詳細講解
Vuex的使用
在Vue中實現(xiàn)集中式狀態(tài)(數(shù)據(jù))管理的一個Vue插件,對vue應用中多個組件的共享狀態(tài)進行集中式的管理(讀/寫),也是一種組件間通信的方式,用于任意組件間的通信
state:存儲數(shù)據(jù)的地址
actions:中轉站,可發(fā)送異步請求增加判斷
mutations:真正改state數(shù)據(jù)的地方
Vuex的使用流程
- 第一步:在state中定義變量
- 第二步:在組件中通過 this.$store.dispatch('actions中定義的函數(shù)'),觸發(fā)actions中得函數(shù)執(zhí)行
- 第三步:在actions中的函數(shù)中,調用context.commit('mutations中定義的函數(shù)')
- 第四步:在mutations中定義的函數(shù)實現(xiàn)真正的修改state中的數(shù)據(jù)
- 第五步:頁面中只要使用$store.state.變量,變量變化,頁面就變化 實現(xiàn)了組件間的通信
?? 注意點:
- 在組件中可以直接調用commit觸發(fā)(mutations中定義的函數(shù))
- 在組件中可以直接修改state中定義變量
Vuex的執(zhí)行流程圖
執(zhí)行流程:
- 用戶進行操作,通過dipatch提交一個actions
-actions接收到這個事件之后,在actions中可以執(zhí)行一些異步或者同步操作,然后根據(jù)不同的情況去分發(fā)給不同的mutations
-actions通過commit去觸發(fā)mutations
-最后mutations去更新state數(shù)據(jù)值,state更新之后,就會通知vue渲染頁面并顯示
Vuex偽代碼
export default new Vuex.Store({ state:{ // 存放真正的變量,也就是數(shù)據(jù) }, mutations:{ // 修改state的數(shù)據(jù)操作 }, actions:{ // 可以進行異步同步請求,校驗權限等操作 } })
Vue-router的使用
官方提供的用來實現(xiàn)SPA 的vue插件,有了它以后我們可以寫很多頁面組件,通過地址欄路徑的不同來顯示不同的頁面組件
基本使用流程
- 安裝插件:
cnpm install vue-router@4
- 在src目錄下新建router文件夾內再創(chuàng)index.js文件用來存放路由
- 在index.js中引入插件
import Vue from 'vue'//引入vue import VueRouter from 'vue-router'//引入vue-router路由模塊 Vue.use( VueRouter ) //使用vue-router這個第三方插件
- 創(chuàng)建了一個router對象實例并導出
const routes = [ { path: '/', name: 'home', component: Home }, ] export default router //導出
-入口文件main.js中引入路由實例 router
new Vue({ router, render: h => h(App) }).$mount('#app')
- 在App.vue中用一個 router-view 的組件來給路由一個展示區(qū)域
<template> <div id="app"> <router-view> </router-view> </div> </template>
路由的跳轉
- 在html中使用
router-link組件:可以在不重新加載頁面的情況下更改URl處理URl的生成以及編碼
to:來指定跳轉的鏈接
<div id ="app"> <p> <router-link to="/home">首頁</router-link> </p> </div>
- 在js中使用
當需要先進行權限判斷的時候需要在js中進行跳轉,校驗通過跳轉刪除校驗不通過跳轉到登陸
export default { Todel() { if (usernane) { this.$router.push('/del') }else{ this.$router.push('/login') } } }
攜帶參數(shù)的路由跳轉
- 在請求地址中帶參數(shù)
<div id ="app"> <p> <router-link to="/login/?username=abc&password=123">用戶登陸</router-link> </p> </div>
在跳轉頁面的組件中接受:
this.$route.query.username this.$route.query.password
- 在地址中帶參數(shù)
<div id ="app"> <p> <router-link to="/bookList/1">展示書籍</router-link> </p> </div>
在跳轉頁面的組件中接受:
this.$route.params.
路由嵌套
關鍵字:children
在router/index.js相應的路由中通過關鍵字進行路由的嵌套
const routes = [ { path: '/login', name: 'login', component: Login, children: [ { name: 'books', path: 'books', component: Books }, ]
?? 注意點:
- 必須要在Login組件中加<router-view></router-view>用來渲染子路由
- 只會變更login下router-view包裹的位置
路由守衛(wèi)
- 前置路由守衛(wèi):有時候根據(jù)用戶的權限區(qū)分普通用戶訪問的接口與超級用戶才能訪問的接口,設置守衛(wèi)來區(qū)分
router.beforeEach((to, from, next) => { console.log('前置路由守衛(wèi)', to, from) if (to.name == 'shoppingcart') { let name = localStorage.getItem('name') if (name) { next() } else { alert('不好意思沒有權限') } } else { next() } })
- 后置路由守衛(wèi):用來設置跳轉后頁面標題的名字,根據(jù)頁面組件名字設置頁面名
router.afterEach((to,from)=>{ console.log('后置路由守衛(wèi)',to,from) document.title = to.name })
到此這篇關于Vuex與Vue router的使用詳細講解的文章就介紹到這了,更多相關Vuex與Vue router內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue2使用element-ui,el-table不顯示,用npm安裝方式
這篇文章主要介紹了vue2使用element-ui,el-table不顯示,用npm安裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07vue項目中的public、static及指定不編譯文件問題
這篇文章主要介紹了vue項目中的public、static及指定不編譯文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03vite2.x實現(xiàn)按需加載ant-design-vue@next組件的方法
這篇文章主要介紹了vite2.x實現(xiàn)按需加載ant-design-vue@next組件的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03