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

Don*_*the 6 javascript vue.js vue-router vuejs2

我正在尝试将用户单击注销链接重定向到服务器呈现的注销页面.但是,由于它代表下面的代码,我只是被重定向到默认路径.

我在烧瓶中设置了服务器路由,如下所示:

@app.route('/logout')
def logout():
    logout_user()
    return render_template('login.html')
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

那我需要在路由器中设置路由吗?

export default new Router {
  routes: {
     {
     path: '/'
     component: Default
     children: [
       {
       path: '/logout'
       // no component added here - is it needed?
       }
     ]
  }
}
Run Code Online (Sandbox Code Playgroud)

由于我已经重定向到/ logout,我不确定如何为此路由添加组件会有所帮助,因为我只想转到base/logout并获取服务器返回的.html页面.有任何想法吗?

Dav*_*kor 17

我有一些问题并找到解决方案...在vuejs 2 +中尝试这个

this.$router.push('/logout')
Run Code Online (Sandbox Code Playgroud)


Dec*_*oon 10

通过$router方法操作窗口位置不会导致浏览器执行整页重新加载,这是您在这种情况下所需的.您需要手动设置窗口位置:

window.location.replace('/logout')
Run Code Online (Sandbox Code Playgroud)