【问题标题】:Vue Router, Cannot read property 'push' of undefinedVue路由器,无法读取未定义的属性“推送”
【发布时间】:2020-10-29 11:06:32
【问题描述】:

我正在尝试重定向页面,但出现错误。我认为我的定义是正确的,为什么会出现这个错误?

提前感谢您的帮助,谢天谢地。

路由器.js

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router);

import Homepage from './screens/Homepage';
import Journey from './screens/Journey';

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: Homepage
    },
    {
      path: '/journey',
      component: Journey
    }
  ]
});

export default router;

main.js

import Router from './router';
new Vue({
  store: Store,
  router: Router,
  el: '#app',
  render: h => h(App)
});

我的推送..

this.$router.push('/journey');

【问题讨论】:

  • this.$router.push('/journey')写在哪个文件里,是在组件里吗?
  • 是的,写在首页的一个组件里。 @ShayaUlman
  • 你能把代码贴出来吗?
  • 现已修复。问题是“setTimeout”。我正在删除 setTimeOut,现在正在工作.. 感谢 Shaya 的帮助...

标签: vue.js vuejs2 vue-component vue-router


【解决方案1】:

我认为this 关键字在您编写this.$router.push('/journey'); 的地方未定义。因此,为了避免在块作用域的顶部出现这种情况,您可以将 this 关键字分配给变量,然后使用该变量而不是 this

// Declare this at the top or first line of the function or block scope
let self = this 
.
.
.
.
self.$router.push('/journey');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 2021-09-06
    • 2018-10-17
    • 2019-07-13
    • 2021-06-11
    • 2022-01-10
    • 2020-02-05
    • 2021-03-06
    相关资源
    最近更新 更多