nuxt 中 vuex 路由鉴权 keeplive
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                nuxt 中 vuex 路由鉴权 keeplive
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                文章目錄
- 使用vuex在刷新后保持狀態(tài)
 - 服務(wù)端使用到了store需要 額外配置
 - 在操作相關(guān)數(shù)據(jù)的頁面保存數(shù)據(jù)到cookie
 - 在store下的actions中再次取出相關(guān)數(shù)據(jù)的頁面保存數(shù)據(jù)到store
 
- 配置自己的路由 及 路由鑒權(quán)
 - keepalive
 
使用vuex在刷新后保持狀態(tài)
先在 plugins下新建 localStorage.js
服務(wù)端使用到了store需要 額外配置
配置完這些,只能保證store中的數(shù)據(jù)在客戶端的store使用沒問題,因為是通過sessionStororg(localStorg)來保存的同步,如果在服務(wù)端也要使用的store數(shù)據(jù),還需要 結(jié)合cookie
在操作相關(guān)數(shù)據(jù)的頁面保存數(shù)據(jù)到cookie
changeNav(index,class_id){if(this.$store.state.activeIndex==index){return }// activeIndex在服務(wù)端會使用,先同步到 cookiethis.$cookies.set("activeIndex",index);this.$store.commit("setactiveIndex",index)this.$emit("getArticles",class_id)}在store下的actions中再次取出相關(guān)數(shù)據(jù)的頁面保存數(shù)據(jù)到store
export default {// 這是cookie的勾子函數(shù),在頁面強制刷新時觸發(fā),context 是上下文nuxtServerInit(store, context) {let activeIndex = context.app.$cookies.get("activeIndex") || 0store.commit("setactiveIndex", activeIndex)} }配置自己的路由 及 路由鑒權(quán)
keepalive
在 default.vue中配置
<template><div><Nuxt keep-alive :keep-alive-props="{ include: includeArr }" /></div> </template> <script> import { $routes } from "../router/index"; export default {data() {return {names:[]};},methods: {// 專門處理 獲取需要緩存的路由handleNeedToken(routes) {routes.filter((item) => {if (item.children && item.children.length > 0) {return this.handleNeedToken(item.children);}// console.log(item);if (item.meta.isKeepLive === true) {this.names.push(item.name);}});return this.names},},computed: {includeArr() {return this.handleNeedToken($routes);},}, }; </script>總結(jié)
以上是生活随笔為你收集整理的nuxt 中 vuex 路由鉴权 keeplive的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Vi Notes
 - 下一篇: [CTF][Web][PHP][Java