国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue實現(xiàn)動態(tài)路由【記錄學習的快樂】

這篇具有很好參考價值的文章主要介紹了Vue實現(xiàn)動態(tài)路由【記錄學習的快樂】。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、什么是動態(tài)路由?
2、動態(tài)路由的好處
3、動態(tài)路由如何實現(xiàn)

1、什么是動態(tài)路由?

動態(tài)路由,動態(tài)即不是寫死的,是可變的。我們可以根據(jù)自己不同的需求加載不同的路由,做到不同的實現(xiàn)及頁面的渲染。動態(tài)的路由存儲可分為兩種,一種是將路由存儲到前端。另一種則是將路由存儲到數(shù)據(jù)庫。動態(tài)路由的使用一般結合角色權限控制一起使用。

總結:

1:路由可變,不是寫死的,動態(tài)加載;

2:存儲分兩種:存儲前端、存儲數(shù)據(jù)庫

2、動態(tài)路由的好處

使用動態(tài)路由可以跟靈活,無需手工維護,我們可以使用一個頁面對路由進行維護。如果將路由存儲到數(shù)據(jù)庫,還可以增加安全性。

總結:

1:靈活,無需手工維護;

2:增加安全性

3、動態(tài)路由如何實現(xiàn)

在此以路由存儲在數(shù)據(jù)庫為例
流程:一般我們在登錄的時候,根據(jù)登錄用戶的角色返回此角色可以訪問的頁面的路由,前端將路由存儲到vuex(vuex存儲的數(shù)據(jù)必須可持久的,不要一刷新頁面就不見),我們在路由前置守衛(wèi)處動態(tài)添加拿到的路由,對頁面進行渲染。

1)此為我的router目錄,index.js對路由添加,守衛(wèi)攔截等處理。static-route.js為前端定義的靜態(tài)路由,不需要動態(tài)加載的,如登陸頁面,忘記密碼頁面,404頁面等。

vue 動態(tài)路由,vue.js,前端,javascript

vue 動態(tài)路由,vue.js,前端,javascript

index.js文件

import Vue from 'vue'
import $cookies from 'vue-cookies'
import VueRouter from 'vue-router'
import store from '../store'?
import staticRoute from './static-route.js'???
Vue.use(VueRouter)??
const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes: staticRoute //staticRoute為靜態(tài)路由,不需動態(tài)添加
})?
let isToken = true
router.beforeEach(async (to, from, next) => {
	//定義isToken為true和vuex不為空時添加路由
	if (isToken && store.state.routers.routers.length != 0) {
		//從vuex中獲取動態(tài)路由
		const accessRouteses = await store.state.routers.routers;
		//動態(tài)路由循環(huán)解析和添加
		accessRouteses.forEach(v => {
			v.children = routerChildren(v.children);
			v.component = routerCom(v.component);
			router.addRoute(v); //添加
		})
		isToken = false //將isToken賦為 false ,否則會一直循環(huán),崩潰
		next({
			...to, // next({ ...to })的目的,是保證路由添加完了再進入頁面 (可以理解為重進一次)
			replace: true, // 重進一次, 不保留重復歷史
		})?
	} else {
		if (to.name == null) {
			next("/404")
		} else {
			if (to.meta.title) { //判斷是否有標題
				document.title = to.meta.title //給相應頁面添加標題
			}
			next()
		}?
	}?
})?
function routerCom(path) { //對路由的component解析
	return (resolve) => require([`@/views/${path}`], resolve);
}?
function routerChildren(children) { //對子路由的component解析
	children.forEach(v => {
		v.component = routerCom(v.component);
		if (v.children != undefined) {
			v.children = routerChildren(v.children)
		}
	})
	return children
}??
export default router?

2)登陸成功后將獲取到的動態(tài)路由存儲到vuex

vue 動態(tài)路由,vue.js,前端,javascript

?vuex—>index.js文件

import Vue from 'vue'
import Vuex from 'vuex'
//數(shù)據(jù)持久化
import createPersistedState from "vuex-persistedstate";
?
Vue.use(Vuex)
const routers = {
  namespaced: true,
  state: () => ({
    routers:"",
  }),
  mutations: {
    routers(state, newsdata) {
      state.routers = newsdata
    },
?
  },
  actions: {
    routers(context) {
      context.commit('routers')
    },
  },
  getters: {
    routers(state) {
      console.log("getters", state)
      return state.routers
    },
    
  }
}
?
?
const store = new Vuex.Store({
  modules: {
    routers: routers,
  },
  
  // 數(shù)據(jù)持久化
  plugins: [createPersistedState({
    //key是存儲數(shù)據(jù)的鍵名
    key: 'routersData',
    //paths是存儲state中的那些數(shù)據(jù),如果是模塊下具體的數(shù)據(jù)需要加上模塊名稱,如user.token  
    paths: ["routers.routers"]
  })]
})
?
?
export default store

我的動態(tài)路由模板

//動態(tài)路由
const dynamicRoute = [{
  "path": "/main",
  "name": "main",
  "redirect": "/main/index",
  "component": "main/main.vue",
  "children": [{
      "path": "index",
      "name": "index",
      "component": "index/index.vue",
      "meta": {
        "name": "index",
        "title": "首頁",
        "icon": "el-icon-location",
        "menu":true //true為菜單欄
      }
    },
    {
      "path": "Configuration",
      "name": "Configuration",
      "redirect": "Configuration/route",
      "component": "Configuration/index.vue",
      "roles": ['developer', "admin"], //  developer、admin角色的用戶才能訪問該頁面
      "meta": {
        "title": "配置",
        "icon": "el-icon-location",
        "menu":true
      },
      "children": [{
          "path": "route",
          "name": "route",
          "component": "Configuration/route/index.vue",
          "meta": {
            "title": "菜單",
            "icon": "",
            "menu":true
          },
        }, {
          "path": "user",
          "name": "user",
          "component": "Configuration/user/index.vue",
          "meta": {
            "title": "用戶管理",
            "icon": "el-icon-location",
            "menu":true
          },
        },
        {
          "path": "admin",
          "name": "admin",
          "component": "Configuration/admin/index.vue",
          "meta": {
            "title": "管理員管理",
            "icon": "",
            "menu":true
          },
        },
        
        {
          "path": "userEdit",
          "name": "userEdit",
          "component": "Configuration/user/user-Edit.vue",
          "meta": {
            "title": "編輯用戶",
            "icon": "",
            "menu":false
          },
        },  
      ]
    },
    {
      "path": "check",
      "name": "check",
      "redirect": "check/user",
      "component": "check/index.vue",
      "roles": ['developer', "admin", "check"], //  developer、admin角色的用戶才能訪問該頁面
      "meta": {
        "title": "審核",
        "icon": "el-icon-location",
        "menu":true
      },
      "children": [{
          "path": "user",
          "name": "checkUser",
          "component": "check/check-user/index.vue",
          "meta": {
            "title": "用戶實名審核",
            "icon": "el-icon-location",
            "menu":true
          }
        },
        {
          "path": "enterprise",
          "name": "checkEnterprise",
          "component": "check/check-enterprise/index.vue",
          "meta": {
            "title": "企業(yè)認證審核",
            "icon": "el-icon-location",
            "menu":true
          },
        },
        {
          "path": "checkNormImage",
          "name": "checkNormImage",
          "component": "check/check-norm-image/index.vue",
          "meta": {
            "title": "標準照認證審核",
            "icon": "el-icon-location",
            "menu":true
          },
        },
        {
          "path": "checkHiringJobs",
          "name": "checkHiringJobs",
          "component": "check/check-hiring-Jobs/index.vue",
          "meta": {
            "title": "求職、招聘認證審核",
            "icon": "el-icon-location",
            "menu":true
          },
        }
      ]
?
    }
  ]
}, ]
export default dynamicRoute

路由界面

vue 動態(tài)路由,vue.js,前端,javascript

講一講遇到的坑及注意點

  1. “component”: “check/check-norm-image/index.vue”, 用字符串再在解析,不要像靜態(tài)路由一樣。否則第一次進去可以,刷新就變空白

  2. 此處為重要的一點,直接用next()不行

    next({
          ...to, // next({ ...to })的目的,是保證路由添加完了再進入頁面 (可以理解為重進一次)
          replace: true, // 重進一次, 不保留重復歷史
        })
    

    3)由于添加完路由還會重復執(zhí)行一遍路由守衛(wèi),所有必須確保不要一直死循環(huán)添加路由。否則直接崩潰。這里我用的是isToken變量確保不循環(huán)。文章來源地址http://www.zghlxwxcb.cn/news/detail-732550.html

到了這里,關于Vue實現(xiàn)動態(tài)路由【記錄學習的快樂】的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 前端之vue 根據(jù)菜單自動生成路由(動態(tài)配置前端路由)

    前端之vue 根據(jù)菜單自動生成路由(動態(tài)配置前端路由)

    在需要權限控制的頁面,往往存在根據(jù)用戶來顯示菜單的情況,單獨根據(jù)用戶類型判斷顯然不是很好,如果后面用戶類型發(fā)生變化,項目修改維護可能就會比較麻煩,所以比較好的做法是根據(jù)后端返回的菜單動態(tài)生成頁面路由,以達到完全權限控制的目的,并且若權限發(fā)生變

    2024年04月10日
    瀏覽(28)
  • Vue.js:Vue-Router動態(tài)路由從服務器接口獲取路由數(shù)據(jù)

    文檔 https://v3.router.vuejs.org/zh/installation.html 版本號 有幾種方式實現(xiàn)動態(tài)路由: 前端配置 完整路由 ,通過接口返回的數(shù)據(jù)判斷是否可顯示,是否可訪問 前端配置 部分路由 ,由后端接口返回的數(shù)據(jù)生成新路由 拋開路由的思維,是否能直接通過 url查詢參數(shù) 或者是 動態(tài)路徑參數(shù)

    2024年02月08日
    瀏覽(86)
  • 路由原理及vue實現(xiàn)動態(tài)路由

    在前端開發(fā)中,路由通常用于實現(xiàn) SPA 應用程序,即在一個頁面中切換不同的內容或頁面,而不需要重新加載整個頁面。路由的實現(xiàn)原理是通過監(jiān)聽 URL 的變化,然后根據(jù)不同的 URL 加載不同的內容或頁面。 在前端框架中,例如 Vue,路由通常是通過路由庫來實現(xiàn)的。路由庫提

    2024年02月06日
    瀏覽(19)
  • vue 實現(xiàn)動態(tài)路由

    vue 實現(xiàn)動態(tài)路由

    vue-router對象中的 addRoutes ,用它來動態(tài)添加路由配置 格式: 舉個例子: 我是以 vue-admin-template 為例,做如下演示: 在router/index.js中的路由配置中 只保留靜態(tài)路由(因為我們要動態(tài)的添加)) 在permission.js中引入,并使用addRoutes動態(tài)添加 這個是 router 下的 index.js中定義的 靜態(tài)

    2024年02月01日
    瀏覽(23)
  • Vue實現(xiàn)動態(tài)路由

    Vue實現(xiàn)動態(tài)路由

    目錄 前言 一、項目介紹 1.開發(fā)環(huán)境 2.功能 3.項目運行截圖?? 二、實現(xiàn) 1.動態(tài)路由如何實現(xiàn) 2.項目目錄介紹 3.核心代碼 4.坑和知識點 小結 最近在學權限相關的管理項目,前端用到了動態(tài)路由,就是根據(jù)用戶角色顯示不同的菜單,動態(tài)添加路由。說著好像很簡單,但看了很多

    2024年02月06日
    瀏覽(18)
  • 【前端】在Vue2中使用Vanta.js炫酷動態(tài)背景(全屏背景)

    【前端】在Vue2中使用Vanta.js炫酷動態(tài)背景(全屏背景)

    官網(wǎng):https://www.vantajs.com/ 由于博主在參考官網(wǎng)及官方GitHub進行應用時遇到一些問題,因此寫了該篇博客,以避免大家因找Bug而浪費時間,方便快速的應用。 注意版本 Vue的版本如下 “vue”: “^2.6.14” 渲染容器 導包 方法 完整代碼: 完整代碼: 參加顏色參數(shù) 如果報錯:‘X

    2024年02月11日
    瀏覽(33)
  • vue實現(xiàn)動態(tài)路由添加(簡單無廢話版本)

    最近練習vue的項目,有關于后臺管理系統(tǒng)的動態(tài)添加路由部分,根據(jù)思路實現(xiàn)了基本的功能,在這里記錄一下,等后面學習后在進行優(yōu)化。 這里只是記錄我個人最后實現(xiàn)的思路,本人由于是初學者,可能思路和代碼有不正確地方,還求多見諒。也請能不吝賜教,一同進步。 我

    2023年04月08日
    瀏覽(21)
  • Vue從后端取數(shù)據(jù),實現(xiàn)動態(tài)路由

    Vue從后端取數(shù)據(jù),實現(xiàn)動態(tài)路由

    ? ? ? ? 將獲取菜單的方法放在全局中,以便每次刷新頁面時,能夠加載出。 this.$store.state.userInfo 是登入后存放在Vuex的用戶信息 ?按照上面要求即可實現(xiàn)

    2024年01月20日
    瀏覽(23)
  • Vue如何實現(xiàn)權限管理(動態(tài)路由addRoutes)

    目錄 引言 推薦B站視頻 一、權限管理 二、控制權限 接口權限 路由權限控制 方法一 方法二 菜單權限 方法一 方法二 按鈕權限 方法一 方法二 項目中會遇到權限管理,來讓特定用戶有特定權限的場景,那么怎么做這個權限管理,以及有多少辦法呢,下面給大家絮叨 vue權限管

    2023年04月25日
    瀏覽(17)
  • vue2+antd——實現(xiàn)動態(tài)菜單路由功能——基礎積累

    vue2+antd——實現(xiàn)動態(tài)菜單路由功能——基礎積累

    最近在寫后臺管理系統(tǒng),遇到一個需求就是要將之前的靜態(tài)路由改為動態(tài)路由,使用的后臺框架是: vue-antd-admin 然后通過 loadRoutes 方法來實現(xiàn)異步動態(tài)路由。 如上圖所示,需要在登錄接口調用成功后,書寫以下的代碼: import { loadRoutes } from \\\'@/utils/routerUtil.js\\\'; import { getCodeL

    2024年02月08日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包