使用vue.js路由踩到的一个坑Unknown custom element

本文解决了一个关于Vue路由组件在require.js环境下无法正确显示的问题。原因是未手动调用Vue.use(VueRouter)来注册路由插件。在AMD环境中,由于Vue对象不会暴露到全局window中,因此需要显式注册。

      在配合require.js使用vue路由的时候,遇到了路由组件报错:

   “vue.js:597 [Vue warn]: Unknown custom element: <router-link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.”

      vue.js的功能是好的,vue-route.js没有起作用,这是怎么回事?明明文件也被加载进来了呀。

//配置路由
define('route',['VueRouter','$a','$b'],function(VueRouter,$a,$b){

    //配置路由地址
    routes = [{
    	path:"/a",
    	component:$a
    },{
    	path:"/b",
    	component:$b
    },{
        path:"/",
        component:$a
    }];

    //创建路由实例,导入配置的路由地址
    var router = new VueRouter({routes});
    return router;


});


    到网上查,到书上找,原来是没有手动调用Vue.use(VueRouter)。以前习惯了在文件头部直接引入vue.js和vue-router.js,这种方式下,在vue-router内部会检测window.Vue对象是否存在,如果存在就会自动调用Vue.use()方法,否则需要手动调用Vue.use(VueRouter)来确保路由插件注册到Vue中。在支持AMD环境中,Vue对象并不会暴露到全局window对象中,而是会通过define()形式输出和引入,因此需要手动注册。

//配置路由
define('route',['Vue','VueRouter','$a','$b'],function(Vue,VueRouter,$a,$b){

    //模块化引入必须明确使用use方法!
    Vue.use(VueRouter);
    //配置路由地址
    routes = [{
    	path:"/a",
    	component:$a
    },{
    	path:"/b",
    	component:$b
    },{
        path:"/",
        component:$a
    }];

    //创建路由实例,导入配置的路由地址
    var router = new VueRouter({routes});
    return router;


});

组件套用导致的错误:permission.js:32 [Vue warn]: Unknown custom element: <ContactsDialog> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <CustomerAdd> at src/components/Dialog_Sales_Addedit/CustomerAdd.vue <ContactsDialog> at src/components/Dialogs_Sales_Choose/CustomerDialog.vue <ContactAdd> at src/components/Dialog_Sales_Addedit/ContactAdd.vue <ContactsDialog> at src/components/Dialogs_Sales_Choose/ContactsDialog.vue <AuthorizationManagement> at src/views/DepartmentSales/serviceCenter/authorizationManagement.vue <AppMain> at src/layout/components/AppMain.vue <Layout> at src/layout/index.vue <App> at src/App.vue <Root> warn @ vue.runtime.esm.js:620 createElm @ vue.runtime.esm.js:5922 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 updateChildren @ vue.runtime.esm.js:6200 patchVnode @ vue.runtime.esm.js:6303 patch @ vue.runtime.esm.js:6466 Vue._update @ vue.runtime.esm.js:3936 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 run @ vue.runtime.esm.js:4542 flushSchedulerQueue @ vue.runtime.esm.js:4298 eval @ vue.runtime.esm.js:1979 flushCallbacks @ vue.runtime.esm.js:1905 Promise.then timerFunc @ vue.runtime.esm.js:1932 nextTick @ vue.runtime.esm.js:1989 queueWatcher @ vue.runtime.esm.js:4390 update @ vue.runtime.esm.js:4532 notify @ vue.runtime.esm.js:731 reactiveSetter @ vue.runtime.esm.js:1056 eval @ vue-router.esm.js:2504 eval @ vue-router.esm.js:2503 updateRoute @ vue-router.esm.js:1990 eval @ vue-router.esm.js:1868 eval @ vue-router.esm.js:1977 step @ vue-router.esm.js:1707 step @ vue-router.esm.js:1714 step @ vue-router.esm.js:1714 runQueue @ vue-router.esm.js:1718 eval @ vue-router.esm.js:1972 step @ vue-router.esm.js:1707 eval @ vue-router.esm.js:1711 eval @ vue-router.esm.js:1957 eval @ vue-router.esm.js:1750 eval @ vue-router.esm.js:1826 Promise.then eval @ vue-router.esm.js:1773 eval @ vue-router.esm.js:1794 eval @ vue-router.esm.js:1794 flatMapComponents @ vue-router.esm.js:1793 eval @ vue-router.esm.js:1729 iterator @ vue-router.esm.js:1936 step @ vue-router.esm.js:1710 step @ vue-router.esm.js:1714 step @ vue-router.esm.js:1714 eval @ vue-router.esm.js:1711 eval @ vue-router.esm.js:1957 eval @ permission.js:32 eval @ regenerator.js:70 eval @ regeneratorDefine.js:13 asyncGeneratorStep @ asyncToGenerator.js:8 _next @ asyncToGenerator.js:22 eval @ asyncToGenerator.js:27 eval @ asyncToGenerator.js:19 eval @ permission.js:69 iterator @ vue-router.esm.js:1936 step @ vue-router.esm.js:1710 runQueue @ vue-router.esm.js:1718 confirmTransition @ vue-router.esm.js:1965 transitionTo @ vue-router.esm.js:1867 replace @ vue-router.esm.js:2280 eval @ vue-router.esm.js:1951 eval @ permission.js:47 eval @ regenerator.js:70 eval @ regeneratorDefine.js:13 asyncGeneratorStep @ asyncToGenerator.js:8 _next @ asyncToGenerator.js:22 Promise.then asyncGeneratorStep @ asyncToGenerator.js:13 _next @ asyncToGenerator.js:22 Promise.then asyncGeneratorStep @ asyncToGenerator.js:13 _next @ asyncToGenerator.js:22 eval @ asyncToGenerator.js:27 eval @ asyncToGenerator.js:19 eval @ permission.js:69 iterator @ vue-router.esm.js:1936 step @ vue-router.esm.js:1710 runQueue @ vue-router.esm.js:1718 confirmTransition @ vue-router.esm.js:1965 transitionTo @ vue-router.esm.js:1867 init @ vue-router.esm.js:2495 beforeCreate @ vue-router.esm.js:540 invokeWithErrorHandling @ vue.runtime.esm.js:1853 callHook @ vue.runtime.esm.js:4207 Vue._init @ vue.runtime.esm.js:4988 Vue @ vue.runtime.esm.js:5069 eval @ main.js:56 ./src/main.js @ app.js:7398 __webpack_require__ @ app.js:849 fn @ app.js:151 1 @ app.js:9266 __webpack_require__ @ app.js:849 checkDeferredModules @ app.js:46 (anonymous) @ app.js:925 (anonymous) @ app.js:928 permission.js:32 [Vue warn]: Unknown custom element: <ContactsDialog> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <CustomerAdd> at src/components/Dialog_Sales_Addedit/CustomerAdd.vue <ContactsDialog> at src/components/Dialogs_Sales_Choose/CustomerDialog.vue <AuthorizationManagement> at src/views/DepartmentSales/serviceCenter/authorizationManagement.vue <AppMain> at src/layout/components/AppMain.vue <Layout> at src/layout/index.vue <App> at src/App.vue <Root>
09-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值