在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 使用addRoutes()方法添加路由后,為什么我Home頁面的導(dǎo)航欄卻沒有刷出

使用addRoutes()方法添加路由后,為什么我Home頁面的導(dǎo)航欄卻沒有刷出來?

最近在做動態(tài)的菜單欄,但是發(fā)現(xiàn)VUE在生成實例的時候已經(jīng)把router加載了,但是我想在用戶登錄之后,從后臺的數(shù)據(jù)庫里面查詢到其擁有的菜單權(quán)限,然后返回到前端去動態(tài)加載路由,隨后發(fā)現(xiàn)網(wǎng)上說vue-router2.2之后的addRoutes()是可以實現(xiàn)這個功能的,隨機試了一下,但是出現(xiàn)了寫問題,希望大神提點
我想現(xiàn)在前端測試下addRoutes()可以實現(xiàn)否,就打算手動給他傳一個路由格式的內(nèi)容,看看能不能行
我在main.js里面調(diào)用addRoutes()方法

const router = new VueRouter({   //全局變量router:routes
  mode:'history',
  routes
})

var a = [];
a.push(
       {
             path: '/',
             component: require('@/views/Home.vue'),
             name: '系統(tǒng)設(shè)置',
             iconCls: 'fa fa-cogs',
             children: [
                   { path: '/roleSetting', pri:['R_ADMIN','R_TRA','R_LAN','R_WAT','R_ENT'],component: require('@/views/systemSetting/roleSetting.vue'), name: '角色管理' }
             ]
        },
            {
        path: '/404',
        component: require('@/views/404.vue'),
        name: '',
        hidden: true
    },
    {
        path: '*',
        hidden: true,
        redirect: { path: '/404' }
    }
);
router.addRoutes(a);

router.beforeEach((to, from, next) => {

  //console.log('main.js '+to.path);

  if(!Vue.prototype.$userInfo || !Vue.prototype.$token){
    var c  = getCookie('userInfo');
    var ct  =   getCookie('token');
    if(c&&c!="")  Vue.prototype.$userInfo = JSON.parse(c);
    if(ct&&ct!="")  Vue.prototype.$token = ct;

    console.log('before route');
    console.log(Vue.prototype.$userInfo);
    console.log(Vue.prototype.$token);
  }


  if(to.path == '/login'){
    delete Vue.prototype.$userInfo;
    delete Vue.prototype.$token;
    delCookie('token','userInfo');
  }

  var token = getCookie('token');
  if( to.path != '/login' && token=="")
      next({ path: '/login' })
  else
      next()
})

router.afterEach(transition => {
// NProgress.done();
});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

我的routes.js

import Login from './views/Login.vue'
import Home from './views/Home.vue'
let routes = [
    {
        path: '/login',
        component: Login,
        name: '',
        hidden: true
    },
    {
        path: '/',
        component: Home,
        name: '',
        iconCls: 'fa fa-home',
        leaf: true,//只有一個節(jié)點
        children: [
            { path: '/index', component: Index, name: '首頁' }
        ]
    }
]
export default routes;

原來的菜單欄只有首頁一個,我通過addRoutes()添加了一個系統(tǒng)設(shè)置的菜單,刷新菜單欄之后應(yīng)該有首頁和系統(tǒng)設(shè)置這兩個菜單,但為什么我新添加的系統(tǒng)設(shè)置這個菜單是沒有的呢?
我的Home.js

<aside :class="collapsed?'menu-collapsed':'menu-expanded'">
                <!--導(dǎo)航菜單-->
                <el-menu :default-active="$route.path" class="newMenu" @open="handleopen" @close="handleclose" @select="handleselect"
                     unique-opened router v-if="!collapsed"  id="sidebar-hook">
                    <template v-for="(item,index) in $router.options.routes" v-if="!item.hidden">
                        <el-submenu :index="index+''" v-if="!item.leaf">
                            <template slot="title"><i :class="item.iconCls"></i>{{item.name}}</template>
                            <el-menu-item v-for="child in item.children" :index="child.path" :key="child.path" v-if="!child.hidden &&child.pri&& child.pri.indexOf($userInfo.roleType)!=-1">{{child.name}}</el-menu-item>
                        </el-submenu>
                        <el-menu-item v-if="item.leaf&&item.children.length>0" :index="item.children[0].path"><i :class="item.iconCls"></i>{{item.children[0].name}}</el-menu-item>
                    </template>
                </el-menu>
                <!--導(dǎo)航菜單-折疊后-->
                <ul class="el-menu el-menu-vertical-demo collapsed" v-show="collapsed" ref="menuCollapsed">
                    <li v-for="(item,index) in $router.options.routes" v-if="!item.hidden" class="el-submenu item">
                        <template v-if="!item.leaf">
                            <div class="el-submenu__title" style="padding-left: 20px;" @mouseover="showMenu(index,true)" @mouseout="showMenu(index,false)"><i :class="item.iconCls"></i></div>
                            <ul class="el-menu submenu  submenu-scroll-hook" :class="'submenu-hook-'+index" @mouseover="showMenu(index,true)" @mouseout="showMenu(index,false)"> 
                                <li v-for="child in item.children" v-if="!child.hidden" :key="child.path" class="el-menu-item" style="padding-left: 40px;" :class="$route.path==child.path?'is-active':''" @click="$router.push(child.path)">{{child.name}}</li>
                            </ul>
                        </template>
                        <template v-else>
                            <li class="el-submenu">
                                <div class="el-submenu__title el-menu-item" style="padding-left: 20px;height: 56px;line-height: 56px;padding: 0 20px;" :class="$route.path==item.children[0].path?'is-active':''" @click="$router.push(item.children[0].path)"><i :class="item.iconCls"></i></div>
                            </li>
                        </template>
                    </li>
                </ul>
            </aside>

運行程序之后,發(fā)現(xiàn)菜單欄沒有變化,但是手動輸入添加的地址是可以跳轉(zhuǎn)到頁面的
圖片描述
圖片描述

回答
編輯回答
我不懂

1.addroute已經(jīng)成功了,所以可以跳轉(zhuǎn)到新增的路由頁。
2.渲染menu不應(yīng)該遍歷路由去生成,因為addroutes后,路由雖然增加了,但路由不是響應(yīng)數(shù)據(jù)(未觀察,且未訂閱),是不會對你的視圖觸發(fā)變化的。

建議解決方案:
使用vuex,對路由信息進行狀態(tài)管理,把初始的路由數(shù)據(jù)存到store里,menu依靠store進行渲染及更新,addroutes后再把新增路由push進store存儲的路由數(shù)組中,即可以觸發(fā)menu更新。希望能解決你的問題。

2018年4月29日 14:19
編輯回答
舊酒館

addroutes只是在路由里加了新的路由,也就是你可以通過url跳轉(zhuǎn),并不是個你添加新的菜單,加新的菜單還需要你自己根據(jù)權(quán)限判斷,寫代碼處理

2018年7月27日 07:29
編輯回答
你好胸

你好,我也是顯示不出來,我看了,你才用樓上說法,已經(jīng)解決了,我看明白了,但是這些方法我都不會用,可以給我看看代碼!106157421@qq.com,謝謝

2017年9月8日 15:42
編輯回答
命多硬
// store 里面 把 根據(jù)權(quán)限過濾的異步路由和不需要權(quán)限的路由結(jié)合在一起,組成新的
SET_ROUTERS: (state, routers) => {
  state.addRouters = routers
  state.routers = constantRouterMap.concat(routers)
}
//view/layout/component/sildbar/index.js
// 替換
<sidebar-item v-for="route in routers" :key="route.name" :item="route" :base-path="route.path"/>

// 引入
...mapGetters([
  'sidebar',
  'routers'
]),
2017年3月5日 08:18