记录一下:
使用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 inHomeContent.vue
. Not that inmain.js
, you need to passrouter
:new Vue({ router, render: h => h(App) }).$mount('#app')
具体实现:
You need to pass
router
data to Vue componentimport 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) });