Vue.js: Failed to mount component: template or ren

2019-07-21 19:05发布

问题:

I'm building my first app with Vue.js 2, and can't figure out the error.

I'm using full build (webpack.config.js):

 resolve: {
        alias: {
            'vue$': 'vue/dist/vue.js',
            "vueRouter$": "vue-router/dist/vue-router.js"
        },
        extensions: ['*', '.js', '.vue', '.json']
    },

My html:

<div id="app">
    <adv-component></adv-component>
</div>

My main.js:

const Vue = require('vue');
var vueComponent = require('./component.vue');
var app = new Vue({
    el: '#app', 
    data: {

    },
    // Local registration
    components: {
        'adv-component': vueComponent,
    }
});

component.vue:

<template id='asd'>
    <div>
        Hello Single File Component!
    </div> 
</template>

<script>
    export default {     
        data () {
            return 0;
        }
    }  
</script>

The error:

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <AdvComponent>

How can I fix the error?

回答1:

A Vue component is usually exported with export default { /* ... */} so it facilitates the default import like import Component from './component.vue'(ES6 syntax)

When using require() (CommonJS) you have to specify you are requiring the default export:

var vueComponent = require('./component.vue').default;