vue-router: Could not lazy loading component, import work fine
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter)
const Home = resolve => require(['./component/home/home.component'], resolve)
const About = resolve => require(['./component/about/about.component'], resolve)
export default new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{path: '/', component: Home},
{path: '/about', component: About},
{path: '*', redirect: '/'}
]
})
error:
vue.js:2532 [Vue warn]: Failed to mount component: template or render function not defined. (found in anonymous component - use the "name" option for better debugging messages.)
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 18 (13 by maintainers)
You used
export defaultinside the component, which only works withimport. When yourequireit, it will give you an object of{default: (your component)}So change the lazy load from
to
Yes and no. If you took a look at the compiled result, you would see something like
exports.default = .... This is to distinguish ES6export defaultfrom traditional CommonJSmodule.exports, using the older syntaxexportsof node. This was introduced in babel 6 if memory serves.You are requiring .template file which is not registered in your webpack config