VueJS 与 vue-router 如何重定向到服务器路由

VueJS with vue-router how to redirect to a server route

我正在尝试将用户点击注销 link 重定向到服务器呈现的注销页面。但由于它与下面的代码一致,我只是被重定向到默认路径。

我在 flask 中设置了如下服务器路由:

@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?
       }
     ]
  }
}

因为我已经重定向到 /logout,所以我不确定为此路由添加一个组件会有什么帮助,因为我只想转到基础 /logout 并获取 .html 返回的页面服务器。有什么想法吗?

通过 $router 方法操作 window 位置不会导致浏览器执行完整的页面重新加载,这正是您在本例中想要的。您需要手动设置 window 位置:

window.location.replace('/logout')

我遇到一些问题并找到解决方案... 在 vuejs 2 +

中试试这个
this.$router.push('/logout')