【问题标题】:Vuejs 2 router not working programatically but, working as router linkVuejs 2 路由器不能以编程方式工作,但作为路由器链接工作
【发布时间】:2019-02-15 14:06:17
【问题描述】:

索引 js 文件

我以编程方式使用 vue 路由器,但它对我不起作用。我在网上搜索,每个人都使用 this.$router.push()

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import BecomeHost from '@/components/BecomeHost'

Vue.use(Router)

export default new Router({
 routes: [
 {
  path: '/',
  name: 'HelloWorld',
  component: HelloWorld
 },
 {
  path: '/become_host',
  name: 'BecomeHost',
  component: BecomeHost
 }
]})

component.vue

当响应成功但不工作时,我像下面这样调用

if (res.data.status === 'success') {
    localStorage.setItem('user', JSON.stringify(res.data.data))
    let user = JSON.parse(localStorage.getItem('user'))
    this.setUserData(user)
    this.$router.push('/become_host')
}

【问题讨论】:

  • 当你说它不起作用时,你能解释一下吗?在做什么或不做什么?它是通过 $router.push 吗?控制台有错误吗?

标签: javascript vue.js


【解决方案1】:

你可以像下面这样试试

this.$router.push({path: 'become_host'})
// or
this.$router.push('BecomeHost')

【讨论】:

    【解决方案2】:

    你可以使用 push 和 props,更多细节here

    在你的情况下,我认为你需要这样的东西:

    this.$router.push({ path: 'become_host' })
    

    this.$router.push({ name: 'BecomeHost' })
    

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2021-07-26
      • 2018-07-13
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 2018-02-18
      • 2017-03-06
      • 2015-11-11
      相关资源
      最近更新 更多