Cannot read property 'component' of undefined 即vue-router 0.x转化为2.x

来源:互联网 发布:网红美图软件有哪些 编辑:程序博客网 时间:2024/06/05 07:51

vue项目原本是用0.x版本的vue-router,但是去报出:Cannot read property 'component' of undefined

这是因为版本问题,由于vue2删除了vue1的内部指令,而vue-router1.x依赖vue的一个内部指令

研究了下vue-router官网,小白我用了接近一天来解决问题,最后我将vue-router改为2.2.0版本

1.打开package.json  将"dependencies"中的   "vue-router"版本改为:"^2.2.0"

2.npm install 

3.在App.vue中

        <a v-link="{path:'/goods'}"></a>

改为 <router-link to="/goods">商品</router-link> 

(这个坑了我很久)

4.然后在main.js中(我的main.js是这样的【2.2.0版本】)

import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './App';
import goods from './components/goods/goods';
import seller from './components/seller/seller';
import ratings from './components/ratings/ratings';


//使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter)
Vue.use(VueRouter);

//定义路由
var routes=[
{path:'/',redirect: '/goods'}, 
{path:'/goods',component:goods},
{path:'/ratings',component:ratings},
{path:'/seller',component:seller}
]

//创建 router 实例,然后传 `routes` 配置
var router=new VueRouter({
linkActiveClass: 'active',
  routes
});
//=> 是ES6的箭头语法
new Vue({
el:'#app',
router,
render:h=>h(App)
})


vue-router官网:https://router.vuejs.org

2 0
原创粉丝点击