2017-04-01 23 views
3

我想重定向用戶單擊註銷鏈接到服務器呈現的註銷頁面。但是,由於它代表下面的代碼,我只是重定向到默認路徑。VueJS與vue-router如何重定向到服務器路由

我在燒瓶內的服務器的路由設置瞭如下所示:

@app.route('/logout') 
def logout(): 
    logout_user() 
    return render_template('login.html') 

在VUE我想重定向路由,這樣我直接到服務器的路由。

<template> 
<a v-on:click="logout">Logout</a> 
</template> 
<script> 
    export default { 
    name: SomePage.vue 
    }, 
    methods: { 
    logout() { 
     this.$router.replace('/logout') 
     // I also tried .go('/logout') 
    } 
    } 
</script> 

那麼我需要在我的路由器中設置路由嗎?

export default new Router { 
    routes: { 
    { 
    path: '/' 
    component: Default 
    children: [ 
     { 
     path: '/logout' 
     // no component added here - is it needed? 
     } 
    ] 
    } 
} 

正如我已經重定向到/註銷,我不知道如何添加該路由的組成部分,將有助於我只想去鹼/註銷,並得到由服務器返回的網頁的.html 。有任何想法嗎?

回答

6

通過$router方法操作窗口位置不會導致瀏覽器執行完整頁面重新加載,這正是您在這種情況下需要的。你需要手動設置窗口位置:

window.location.replace('/logout') 
9

我有一些問題,並找到解決辦法... 試試這個在vuejs 2 +

this.$router.push('/logout') 
+0

請編輯這篇文章提供解釋和/或關於該解決方案爲什麼會起作用的其他上下文。 – Toby

+0

@Toby,根據Vue文檔「爲了與HTML5歷史API保持一致,router.go現在僅用於後退/前進導航,而router.push則用於導航到特定頁面。」 https://vuejs.org/v2/guide/migration-vue-router.html希望這有幫助。 –

相關問題