VueJS项目报错解决:router.map is not a function - 方法大全

VueJS项目报错解决:router.map is not a function - 方法大全


问题描述: vue报错 router.map is not a function

原因和解决办法(2种以上):

在vue的高版本中已经弃用了低版本的.map()方法。
可以通过设置向下兼容,或者新的方法,来实现路由页面的激活功能。
.
具体情况,我们通过下面的代码,对比一下不同版本的代码区别,如下:



一、兼容处理方法

Method 1 使用命令npm install vue-router@0.7.13兼容1.0版本vue。


二、基于vue2.0,写对应的方法

  1. vue低版本 [ 旧版本代码 ]
// 部分代码如下:
let router = new VueRouter();

router.map({
	'/goods': {
		component: goodsVue
	},
	'/ratings': {
		component: ratingsVue
	},
	'/seller': {
		component: sellerVue
	}
});

router.start(app, '#app');

router.go('/goodsVue');  // 定义首页

注意: 使用1.0和2.0在一些用法上有比较大的版本差别,所以不同用法!

Method 2 export default写法
  1. vue高版本 [ 弃用了.map()方法 ]
// 部分代码如下:
Vue.use(VueRouter);

export default new VueRouter({
  mode: 'history',
  routes: [
    { // 定义首页
      path: '',
      redirect: {name: 'goods'}
    },
    {
      path: '/',
      name: 'header',
      component: headerVue
    },
    {
      path: '/goods',
      name: 'goods',
      component: goodsVue
    },
    {
      path: '/ratings',
      name: 'ratings',
      component: ratingsVue
    },
    {
      path: '/seller',
      name: 'seller',
      component: sellerVue
    }
  ]
});

Method 3 const 写法

在这里插入图片描述


以上就是关于“ VueJS项目报错解决:router.map is not a function - 方法大全 ” 的全部内容。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值