20
votes

Starting with Vue.js and wanted to give it a try to the example that comes with laravel.

No component is displayed and in console I get

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

found in

---> <Example>
   <Root>

Not a fresh install, upgraded from 5.2->5.3->5.4

resources/assets/js/app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */
Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});

resources/assets/js/components/Example.vue

<template>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Example Component</div>

                <div class="panel-body">
                    I'm an example component!
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

This is the blade in which I have the js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Testing</title>
    <link rel="stylesheet" href="css/app.css">
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
    <body>
    <div id="app">
        <example></example>
    </div>
        <script src="js/app.js" charset="utf-8"></script>
    </body>
</html>

webpack.mix.js

let mix = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
    .sass('resources/assets/sass/app.scss', 'public/css');
8
do you have laravel mixx here? can you show webpack.mix.js ?(in root)cssBlaster21895
Yes, I do have laravel mix and added webpack to the post.Carlos F
another thing, maybe compare your package json with source one - especially the vue version github.com/laravel/laravel/blob/master/package.jsoncssBlaster21895
how about vuejs versions?have you checked already?cssBlaster21895

8 Answers

9
votes

It has to do with the way you are mounting Vue. One time it needs compiler and another time not https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only .

You could try my way (it is pure vue project, made from vue webpack template):

import Vue from 'vue'
import App from './App'
import Example from './components/Example'
import router from './router' //this will import router/index.js

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App, Example }
})

This will mount component in global object.

50
votes

It should be require('./components/Example.vue').default. Since v13, vue-loader exports the component as the default key, which still works the same when using import, but requires the above when using require.

17
votes

Try this

Vue.component('example-component', require('./components/ExampleComponent.vue').default);
12
votes

As of now, the plaster I have is to add a 'default()' to all Vue object's component.

Vue.component('form-component', require('./components/FormComponent').default);
Vue.component('gratitude-pop', require('./components/GratitudePop').default);//..etc..etc

Running this package:

  "devDependencies": {
        "axios": "^0.18",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.5",
        "popper.js": "^1.12",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue": "^2.5.17"
    },
    "dependencies": {
        "vuex": "^3.0.1"
    }

Throw in your thumbs-up for using VUEX ;P

9
votes

I have resolved this issue easily by using .default method at the end of the require().default in the following line.

Vue.component('example-component', require('./components/ExampleComponent.vue').default);
Vue.component('articles', require('./components/ArticleComponent.vue').default);

app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('articles', require('./components/ArticleComponent.vue').default);

const app = new Vue({
    el: '#app', 
});
6
votes

Instead of

Vue.component('example', require ('./components/Example.vue'));

Use this

Vue.component ('example', require ('./components/Example.vue').default);
0
votes
  • delete the folder node_modules
  • run npm install
  • add the following line to package.json

    "webpack": "cross-env NODE_ENV=development webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    
  • npm run webpack

  • npm run watch

  • dont change anything else

0
votes

Just to add a small piece of information on top of the above answers.

The error is fixed if .default is added on the component that throws the error.