6vue路由引入配置报错(转)

引入路由配置文件index.js报如下错:

Error in beforeCreate hook: "TypeError: this._router.init is not a function"

Error in beforeCreate hook: "TypeError: this._router.init is not a function"

Error in render: "TypeError: Cannot read property 'matched' of undefined"

Cannot read property 'matched' of undefined

是因为把vue路由文件单独抽离出来后报错,vue解析文件正确,路由抽离引用也正确。组件都正确。问题是在vue路由JS文件中,暴露变量的格式有误。

错误的格式为:

export default {

router

}

使用这种格式 需要用变量的形式接收,如果你用的是import xx from '../'那么就错了

如果你是使用以上格式 那么正确引用格式为:import {xx} from '../'

当然我们也可以保留import xx from '../'的格式 那么vue路由暴露变量方式改为以下即可
export default router

格式必须如下统一:

格式一 变量形式 --暴露
export default {
router
}

--接收
import {router} from './xx.js'



格式二 直接暴露
 export default router

接收
import router from './xx.js'

原文链接:https://www.jianshu.com/p/9b4ee17f2cba

posted @ 2022-06-22 22:23  青仙  阅读(453)  评论(0编辑  收藏  举报