为什么 router.go() 在 Vue Router 中不起作用?

ste*_*eve 3 javascript vue.js vue-router

使用 vue-router,我有一个组件检查用户登录状态,如何使用路由器重定向到登录页面 URL?

在组件中,我的代码:

<script>
  export default {
    components: {
    },
   
    name: 'landing-page',
    created: function () {
      const router = this.$router
      router.go({path: '/login'})
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

router.go不起作用。

小智 6

请记住,它router.go采用一个整数参数来指示在历史堆栈中向前或向后移动多少步。因此,要给出一条路径,您应该使用router.push('your_url').

欲了解更多信息,我建议您阅读以下内容: Vuerouter


ste*_*eve 5

哦,使用router.push('/login')可以正常工作。