Cannot read property ‘_router‘ of undefined Vue js 或者Cannot read property ‘push‘ of undefined Vue

记录一下:

使用vue3,vue create命令创建的vue项目,在路由的时候出现:

Cannot read property ‘_router‘ of undefined Vue js 或者Cannot read property ‘push‘ of undefined Vue

问题所在:

You need to set up routers in main.js component, not in HomeContent.vue. Not that in main.js, you need to pass routernew Vue({ router, render: h => h(App) }).$mount('#app') 

具体实现: 

You need to pass router data to Vue component

import Vue from 'vue'
import App from './App.vue'
import Router from 'vue-router'

Vue.config.productionTip = false
export const changeRoute = new Vue();

Vue.use(Router) // should place it before new Vue()

// just example
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'ToolDetail',
      component: ToolDetail // place your component here
    }
  ]
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

 修改完后完整的main.js:

import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import Router from 'vue-router'
import routers from './routers.js'
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
Vue.use(ElementUI);
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: routers
})
new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
new Vue({
  router,
  el: '#app',
  render: h => h(App)
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeError: Cannot read property 'call' of undefined错误是指在调用一个未定义的对象的call方法时发生的错误。根据引用和引用[2]的内容,我们可以了解到这个错误通常发生在使用Vue.js路由时,可能是因为路由配置有误导致调用了未定义的函数或方法。 根据引用中提到的错误详情,TypeError通常是由于变量或参数不是预期类型引起的。在Vue.js中,路由配置常常使用到call方法来调用相应的函数,而当调用的对象是undefined时,就会触发该错误。 举个例子,假设在Vue.js路由配置中有如下代码: ```javascript router.beforeEach((to, from, next) => { // 在此处调用了一个不存在的函数 to.meta.beforeEnter.call(); next(); }); ``` 在这个例子中,to.meta.beforeEnter是一个函数对象,但如果这个函数对象未定义,就会产生TypeError: Cannot read property 'call' of undefined错误。 因此,解决这个错误的方法就是确保在路由配置中所调用的函数或方法都是已定义的。可以检查路由配置文件,确保所有使用到call方法的函数都是存在的。另外,也可以在调用函数之前先进行类型判断,避免调用未定义的函数。 总结起来,TypeError: Cannot read property 'call' of undefined错误通常发生在Vue.js路由配置中,是由于调用了未定义的函数或方法导致的。解决这个错误的方法是确保所有调用的函数都是已定义的,并进行必要的类型判断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值