2017-03-17 65 views
1

nprogress在其他方面工作得很好,但在重定向到/登錄時永久旋轉。我試圖showProgressBar:false無濟於事。Vue/Vue-bulma - nprogress:進度條永遠加載守衛/重定向

如果用戶已經登錄,他們將被重定向到/儀表板,如果他們不會被重定向到/登錄。

我的代碼如下所示:

const routes = [ 
    {path: '/', name: 'root', redirect: { name: 'login' }, meta: {showProgressBar: false}}, 
    {path: '/login', component: LoginPage, name: 'login', beforeEnter: loggedIn, meta: {showProgressBar: false}}, 
    {path: '/dashboard', component: DashboardPage, name: 'dashboard', meta: { requiresAuth: true }}, 
    {path: '/editor', component: PhoneEditorPage, name: 'editor', meta: { requiresAuth: true }}, 
    {path: '/usersettings', component: PinPasswordPage, name: 'pinpassword', meta: { requiresAuth: true }}, 
    {path: '/callforwarding', component: CallForwardingPage, name: 'callforwarding', meta: { requiresAuth: true }}, 
    { name: 'dropdown', path: '/dropdown', component: Dropdown, meta: { requiresAuth: true }} 
] 

const router = new VueRouter({ 
    linkActiveClass: 'active', 
    mode: 'hash', 
    routes 
}) 

function loggedIn (to, from, next) { 
    const authUser = JSON.parse(window.localStorage.getItem('authUser')) 
    if (authUser && authUser.auth) { 
    next({name: 'dashboard'}) 
    } else { 
    next() 
    } 
} 

router.beforeEach((to, from, next) => { 
    if (to.meta.requiresAuth) { 
    const authUser = JSON.parse(window.localStorage.getItem('authUser')) 
    if (authUser && authUser.auth) { 
     next() 
    } else { 
     next({name: 'login'}) 
     this.nprogress.done() 
    } 
    } 
    next() 

謝謝您的時間。

+0

你能爲此創建一個jsfiddle嗎? –

+0

這對我所有的依賴項來說並不可行。 –

回答

0

不是簡單的回答,而不在行動中看到的代碼,但是,你可以嘗試反轉調用this.nprogess.done()next(...)這樣的:

router.beforeEach((to, from, next) => { 
    if (to.meta.requiresAuth) { 
    const authUser = JSON.parse(window.localStorage.getItem('authUser')) 
    if (authUser && authUser.auth) { 
     next() 
    } else { 
     this.nprogress.done(); // <- HERE 
     next({name: 'login'}) 
    } 
    } 
    next() 
} 

因爲next()調用移動背景下新的組件,並我不確定在正確的時刻會打電話給nprogress。