欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue addRoutes路由動態(tài)加載操作

 更新時間:2020年08月04日 11:12:16   作者:百科全輸  
這篇文章主要介紹了vue addRoutes路由動態(tài)加載操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

需求:增加權(quán)限控制,實現(xiàn)不同角色顯示不同的路由導(dǎo)航

思路:每次登陸后請求接口返回當前角色路由

核心方法:vue-router2.2.0的addRoutes方法 + vuex

以下是我實現(xiàn)的獲取菜單路由的方法,我將該方法的調(diào)用放在首頁組件的生命鉤子中,即便用戶刷新瀏覽器清空了路由還是會重新調(diào)用接口獲取,不至于會丟失。同時考慮到會有切換用戶的可能,所以不將獲取到的路由信息保存到cookie或者localstorage當中

獲取菜單之前先判斷routerState,避免多次請求, 我這里使用element-ui的導(dǎo)航菜單功能v-for循環(huán)this.myRouter參數(shù)即可顯示動態(tài)路由導(dǎo)航

/**
* 獲取菜單
*/
getMenu () {
 if (this.$store.getters.routerState === false) {
 // 清理已經(jīng)存在的動態(tài)路由
 this.clearDynamicRoute()
 // 更改請求路由狀態(tài)為true
 this.$store.commit('SET_ROUTERSTATE', true)
 getMyMenu().then((res) => {
  if (res.code === '0') {
  // 格式化路由,將數(shù)據(jù)轉(zhuǎn)為addRoutes可接受的route格式數(shù)組
  let myMenu = this.formatMenu(res.data.menu)
  if (myMenu.length <= 0) { // 沒有動態(tài)路由
   return
  }
  for (let index = 0; index < myMenu.length; index++) {
   // 將請求的路由先存放到options對象中
   this.$router.options.routes.push(myMenu[index])
  }
  // 將完整需要顯示的路由添加進去
  this.$router.addRoutes(this.$router.options.routes)
  // 這里將路由顯示在頁面上
  this.MyRouter = this.$router.options.routes
  }
  // 在這里就可以打印出新路由
  console.log(this.$router)
 })
 }
}

補充知識:vue+element 進入不同路由頁面(二級頁面),讓相應(yīng)的左側(cè)菜單

路由配置

{
 path: '/finance',
 name: 'Finance',
 meta: {
 title: '財務(wù)'
 },
 component: () =>
 import('@/components/Finance'),
 redirect: '/finance/code/code',
 children: [{
 path: '/finance/code',
 name: 'financeindex',
 meta: {
 title: '稅收配置'
 },
 redirect: '/finance/code/code',
 component: () =>
 import('@/components/finance/financeindex'),
 children: [{
 path: '/finance/code/code',
 name: 'FinanceCode',
 hidden: false,
 active:'/finance/code', //這里是在左側(cè)菜單顯示并且需要選中的
 meta: {
  title: '稅收編碼(金稅)'
 },
 component: () =>
  import('@/components/finance/code/Code'),
 },
 {
 path: '/finance/code/codeimportrecord',  // 這個路由進入的界面是 稅收編碼(金稅)的二級頁面, 當進入這個頁面的時候,需要菜單中的稅收編碼(金稅)顯示選中
 name: 'FinanceCodeImportRecord',
 hidden: true,
 meta: {
 title: '稅收編碼導(dǎo)入記錄'
   },
 component: () =>
 import('@/components/finance/code/CodeImportRecord'),
 },
 {
 path: '/finance/classcode/classcode',
 name: 'FinanceClassCode',
 hidden: false,
 active:'/finance/classcode', //為了省事,只給需要在左側(cè)顯示的路由添加active屬性
 meta: {
  title: '分類稅收編碼確認'
 },
 component: () =>
  import('@/components/finance/classCode/ClassCode'),
 },
 ]
 }, ]

element

<template>
 <div class="leftnav">
 <!--<div class="" v-for="nav in navs">
   <div class="LiHeader">
    {{nav.name}}
   </div>
   <li v-for="item in nav.san">
    {{item.name}}
   </li>
  </div>-->
 <el-menu
  style="text-align: left;"
  :default-active=this.show // 這是的值是指與 el-menu-item中:index的值對應(yīng)的那一天顯示選中(正常情況就是一個頁面一個路由,進入那個路由,對應(yīng)的導(dǎo)航菜單需要選中)
  class="el-menu-vertical-demo"
  @open="handleOpen"
  @close="handleClose"
  background-color="#282b33"
  text-color="#bcbcbc"
  active-text-color="#ffffff">
  <div class="" v-for="(nav,index) in navs" :key="index" style="width: 200px;">
  <div class="" style="color: #ffffff;height: 40px;line-height: 40px;padding-left: 20px;font-size: 16px;">
   {{nav.meta.title}}
  </div>
  <el-menu-item @click="clickroute(item.path)" v-for="(item,index) in nav.children" v-if="!item.hidden" :key="index" :index="item.active"(這里原來是item.path) style="height: 40px;line-height: 40px;">{{item.meta.title}}</el-menu-item>
  </div>

 </el-menu>
 </div>
</template>
<script>

js

data() {
  return {
  navs:[],
  show:null //初始化上面:default-active綁定的值
  }
 },
 created() { //// 頁面載入的時候,拿到url,用/拆開,然后拼起來前兩個路徑,并且賦值, 這個時候show對應(yīng)的就是路由表中的 avtive,
  let route=this.$route.path.split('/')
  let vueRouter=this.$router.options.routes.filter(router=>{return router.path=='/'})[0].children
  let filterVuerouter=vueRouter.filter(router=>{return '/'+route[1] == router.path })
  this.navs=filterVuerouter[0].children
  console.log(this.navs)

  let router ='/'+route[1]+'/'+route[2]
 console.log(router)
  this.show=router
//  console.log(this.show)
 },
 mounted() {

 },

以上這篇vue addRoutes路由動態(tài)加載操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue.js初學(xué)入門教程(2)

    vue.js初學(xué)入門教程(2)

    這篇文章主要為大家詳細介紹了vue.js初學(xué)入門教程第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • vue實現(xiàn)頭像上傳功能

    vue實現(xiàn)頭像上傳功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)頭像上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Vue守衛(wèi)零基礎(chǔ)介紹

    Vue守衛(wèi)零基礎(chǔ)介紹

    導(dǎo)航守衛(wèi)就是路由跳轉(zhuǎn)過程中的一些鉤子函數(shù),這個過程中觸發(fā)的這些函數(shù)能讓你有操作一些其他的事兒的時機,這就是導(dǎo)航守衛(wèi),守衛(wèi)適用于切面編程,即把一件事切成好幾塊,然后分給不同的人去完成,提高工作效率,也可以讓代碼變得更加優(yōu)雅
    2022-09-09
  • vue通過子組件修改父組件prop的多種實現(xiàn)方式

    vue通過子組件修改父組件prop的多種實現(xiàn)方式

    這篇文章主要介紹了vue通過子組件修改父組件prop的幾種實現(xiàn)方式,比較常用的方式是通過Prop單向傳遞的規(guī)則,需要的朋友可以參考下
    2021-09-09
  • 又一款MVVM組件 構(gòu)建自己的Vue組件(2)

    又一款MVVM組件 構(gòu)建自己的Vue組件(2)

    這篇文章主要為大家分享了一款MVVM組件,構(gòu)建自己的Vue組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue中關(guān)于@media媒體查詢的使用

    vue中關(guān)于@media媒體查詢的使用

    這篇文章主要介紹了vue中關(guān)于@media媒體查詢的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 詳解Vue使用 vue-cli 搭建項目

    詳解Vue使用 vue-cli 搭建項目

    本篇文章主要介紹了詳解Vue使用 vue-cli 搭建項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 深入理解Vue.js源碼之事件機制

    深入理解Vue.js源碼之事件機制

    本篇文章主要介紹了Vue.js源碼之事件機制,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue 使用 echarts 繪制中國地圖的實現(xiàn)代碼

    vue 使用 echarts 繪制中國地圖的實現(xiàn)代碼

    這篇文章主要介紹了vue 使用 echarts 繪制中國地圖,內(nèi)容包括插入echarts所需模塊及完整的代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • 解決vite項目Uncaught Syntaxerror:Unexpected token>vue項目上線白屏問題

    解決vite項目Uncaught Syntaxerror:Unexpected token>vue項

    這篇文章主要介紹了解決vite項目Uncaught Syntaxerror:Unexpected token>vue項目上線白屏問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論