鍍金池/ 問答/HTML/ Vue-router 使用 addRoutes 沒效果

Vue-router 使用 addRoutes 沒效果

vue-router 中使用addRoutes 添加動態(tài)路由無效.

import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/views/layout'

const _import = comp => require(`@/views/${comp}`).default

Vue.use(Router)

//全局靜態(tài)路由
const constantRouters = [
  { path: '/login', name: 'login', component: _import('commons/login/login'), hidden: true },
  { path: '/error', name: 'error', component: _import('commons/errorPage'), hidden: true }
]
//全局路由守衛(wèi)
router.beforeEach((to, from, next) => {
  let token = getToken()
  //已登錄
  if (token && token.length > 0) {
    //如果沒有獲取 用戶信息
    if (store.getters.user.length < 1) {
    //這里沒有使用 接口中的數(shù)據(jù). 使用數(shù)據(jù)模擬.
      let mainRoutes = [{
        path: '/',
        component: Layout,
        name: 'layout',
        redirect: '/home',
        children: [
          { path: '/home', component: _import('modules/home'), name: 'home', meta: { title: '首頁' } }
        ],
        // 主路由守衛(wèi)
        beforeEnter (to, from, next) {
          let token = getToken()
          if (! token) {
            next({ path: '/login' })
          } else {
            next()
          }
        }
      }]
      router.addRoutes(mainRoutes)
      // next({ ...to, replace: true })
    }
    next()
  }  else {
    //否則直接進入 登錄頁
    next({ path: '/login' })
  }

})

const router = new Router({
  routes: constantRouters
})

export default router

訪問 http://localhost:8080/#/ 會跳轉(zhuǎn)到 http://localhost:8080/#/login 頁面也是正常顯示.
登錄成功后 router.push('/') 到根路徑 頁面是一篇空白.

確定路徑這些都沒有問題.組件都是正常加載到.我實在不知道我錯在哪里...

回答
編輯回答
祈歡

沒有人回答...

2017年3月24日 15:12