Answer the question
In order to leave comments, you need to log in
Why does the "Failed to mount component" error occur?
I'm doing a Laravel + vuejs demo project on a local Apache server.
Created 2 components: App and Home. I run yarn run watch and open the page. I get a blank page
app.js:883 [Vue warn]: Failed to mount component: template or render function not defined.
found in
---> <App> at resources/assets/js/App.vue
<Root>
let mix = require('laravel-mix');
const { VueLoaderPlugin } = require('vue-loader');
mix.webpackConfig({
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader"
}
]
},
plugins: [
new VueLoaderPlugin()
],
});
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
<template>
<div class="panel panel-default">
<div class="panel-heading">
<nav>
<ul class="list-inline">
<li>
<router-link :to="{ name: 'home' }">Home</router-link>
</li>
<li class="pull-right">
<router-link :to="{ name: 'login' }">Login</router-link>
</li> <li class="pull-right">
<router-link :to="{ name: 'register' }">Register</router-link>
</li>
</ul>
</nav>
</div>
<div class="panel-body">
<router-view></router-view>
</div>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
}
}
</script>
<template>
<h1>Laravel 5 Vue SPA Authentication</h1>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
}
}
</script>
import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './App.vue';
import Home from './components/Home.vue';
Vue.use(VueRouter);
const router = new VueRouter({
routes: [
{
path: '/',
name: 'home',
component: Home
},
]
});
new Vue({
el: '#app',
router: router,
render: app => app(App)
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Laravel</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div id="app"></div>
</div>
<script src="/js/app.js"></script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
This is all because of VueRouter, you need to initialize it in /resources/assets/js/app.js like this:
const router = new VueRouter({
routes: [
{
path: '/',
name: 'home',
component: require('./components/Home.vue').default
},
]
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question