2016-12-15 5 views
2

私はVueJSで新しく、私はVueJS 2 Webpackテンプレートを使用しています。 ログインが成功したら、ログインコンポーネントからホームコンポーネントにリダイレクトしようとしていましたが、多くのことを試しましたが、まだエラーが発生しています。Vuejsの特定のルートに移動する2

this.$router.replace(this.$router.go('/')) 

そして、私はこのエラーを得た:

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') 
     } 
    } 
    }] 
}) 

そして、それは私が自宅のコンポーネントにリダイレクトしようとしたものです:ここに は私のルーティングです

Uncaught (in promise) TypeError: Cannot read property 'name' of undefined(…) 

答えて

6

this.$router.replace()は場所を期待しています。

this.$router.go('/')はブラウザの履歴を移動するので、実際には何も返さないはずです。基本的にはwindow.history.go(Number)です。

あなたのページを置き換える場合は、this.$router.replace('/')とします。

また、私はドキュメントのこの部分をチェックしたいと思います。反復コードをたくさん入れてbeforeEachに入れることができます。

https://router.vuejs.org/en/advanced/meta.html

+0

今は問題ありません。どうもありがとうございました。 – jemlifathi

+0

全く問題ありません! –

+0

驚くばかり!魅力的に働いた。 – victorkurauchi

関連する問題