2
的具體路線我是新的VueJS,我使用的VueJS 2 Webpack模板。 我試圖從登錄組件重定向到家庭組件,如果登錄成功,我嘗試了很多東西,但我仍然收到錯誤。 這裏是我的路由:查看在Vuejs 2
const router = new VueRouter({
mode: 'history',
routes: [{
'name': 'home',
path: '/',
component: require('./components/Home.vue'),
beforeEnter: (to, from, next) => {
if (!localStorage.getItem('user')) {
next('/login')
}
}
}, {
'name': 'profile',
path: '/profile',
component: require('./components/Profile.vue'),
beforeEnter: (to, from, next) => {
if (!localStorage.getItem('user')) {
next('/login')
}
}
}, {
'name': 'login',
path: '/login',
component: require('./components/Login.vue')
}, {
'name': 'new',
path: '/new',
component: require('./components/New.vue'),
beforeEnter: (to, from, next) => {
if (!localStorage.getItem('user')) {
next('/login')
}
}
}, {
'name': 'signup',
path: '/signup',
component: require('./components/Signup.vue')
}, {
'name': 'logout',
path: '/logout',
beforeEnter: (to, from, next) => {
localStorage.removeItem('user')
next('/login')
}
}, {
'name': 'article',
path: '/article/:id',
component: require('./components/Article.vue'),
beforeEnter: (to, from, next) => {
if (!localStorage.getItem('user')) {
next('/login')
}
}
}]
})
而這正是我試圖重定向到家裏組件:
this.$router.replace(this.$router.go('/'))
而且我得到了這個錯誤:
Uncaught (in promise) TypeError: Cannot read property 'name' of undefined(…)
現在工作正常!非常感謝你。 – jemlifathi
完全沒問題! –
太棒了!像魅力一樣工作。 – victorkurauchi