Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 Vue.JS Failed to mount component: template or render function not defined

Tags:

laravel

vue.js

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');
like image 282
Carlos F Avatar asked Sep 07 '17 18:09

Carlos F


3 Answers

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.

like image 148
Abhishek kushwaha Avatar answered Oct 10 '22 13:10

Abhishek kushwaha


Try this

Vue.component('example-component', require('./components/ExampleComponent.vue').default);
like image 45
Golam Sorwar Avatar answered Oct 10 '22 11:10

Golam Sorwar


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

like image 12
clusterBuddy Avatar answered Oct 10 '22 13:10

clusterBuddy