vue-router 采坑记录

一、错误:Uncaught Error: [vue-router] route config "component" for path: /detail cannot be a string id. Use an actual component instead.

 解决方案:路由配置中

import Detail from './components/detail.vue';

const routers = [
  ......
    {
        path:'/detail',
        name:'detail',
        component:'Detail' //这里是组件名称,不能加引号!!!!去掉引号,问题解决。
    },

......
]

二、警告:vue-router.esm.js?8c4f:16 [vue-router] Route with name 'detail' does not exist

解决方案:

const router = new VueRouter({
    mode: 'history',
    routes//即routes:routes的简写形式----注意这里,是routes,必须是routes,这是一个固定的属性。
})

三、错误:vue-router.esm.js?8c4f:2065 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location:

解决方案:重写push方法,加上catch处理,防止报错

const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

四、动态参数问题:连接上带着参数和不带参数的配置

URL上带参:

  {
      path: '/:msg',//params传参,设置动态参数,链接上会有这个参数
     name:'home',
     component: Home
   },

URL上不带参:

 {
        path: '/',params传参,没有设置动态参数,链接上不会有这个参数,刷新页面参数丢失    
         name:'home',
        component: Home
    }

四、[Vue warn]: Extraneous non-props attributes (class) were passed to component but could not be 
automatically inherited because component renders fragment or text root nodes. 
  at <Users class="dash-ht" onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
  at <RouterView class="dash-ht" > at <App>

[Vue warn]: Extraneous non-props attributes (class) were passed to component but could not be 
automatically inherited because component renders fragment or text root nodes. 
  at <Users class="dash-ht" onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
  at <RouterView class="dash-ht" > at <App>

这个错误 是在vue文件中的<template></template>标签中有多个根元素造成的,比如

 

<template>
    <div>
        这是第一个根元素
    </div>
    <div>
        这是第二个根元素
    </div>
</template>

 解决办法就是用一个div包裹这两个div

<template>
    <div>
       这个才是根元素,把下面两个包裹起来
        <div>
            这是第一个根元素
        </div>
        <div>
            这是第二个根元素
        </div>
    </div>
</template>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值