简体   繁体   English

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

[英]VueJS with vue-router how to redirect to a server route

I am trying to redirect a user clicking logout link to the server rendered logout page. 我正在尝试将用户单击注销链接重定向到服务器呈现的注销页面。 But as it stands with code below I just get redirected to the default path. 但是,由于它代表下面的代码,我只是被重定向到默认路径。

I have a server route set-up in flask as the following: 我在烧瓶中设置了服务器路由,如下所示:

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

In vue I want to redirect the route so that I am directed to the server route. 在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>

Then do I need to set up a route in my router? 那我需要在路由器中设置路由吗?

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

As I have already redirected to /logout, I am not sure how adding a component for this route would help as I just want to go to the base /logout and get the .html page returned by the server. 由于我已经重定向到/ logout,我不确定如何为此路由添加组件会有所帮助,因为我只想转到base / logout并获取服务器返回的.html页面。 Any ideas? 有任何想法吗?

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

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

Manipulating the window location via $router methods doesn't cause the browser to perform a full page reload, which is what you want in this case. 通过$router方法操作窗口位置不会导致浏览器执行整页重新加载,这是您在这种情况下所需的。 You'll need to set the window location manually: 您需要手动设置窗口位置:

window.location.replace('/logout')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM