VueJS Router 'Failed to mount component: template or render function not defined.'

强颜欢笑 提交于 2019-12-11 07:02:23

问题


I'm following this video: https://laracasts.com/series/learn-vue-2-step-by-step/episodes/26?autoplay=true

My initial problem is described here, but that was resolved. I include it here so you can see what steps I've taken so far.

Now, I'm getting this warning: [Vue warn]: Failed to mount component: template or render function not defined.

A warning is not so bad, but the router-view is not showing up. That is, there is nothing showing up below the Home and About links.

master.blade.php

<!doctype html>
    <html lang="en">
    <head>
        <title>My App</title>
        <link rel="stylesheet" href="/css/app.css">
    </head>
    <body>
        <div id="app">
            <router-link to="/">Home</router-link>
            <router-link to="/about">About</router-link>

            <router-view></router-view>
        </div>
        <script src="/js/app.js"></script>
    </body>
</html>

Home.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">Home Page</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>

Any help would be appreciated. Thanks!

[Edit 1]

resources/assets/js/app.js

import './bootstrap';
import router from './routes';


new Vue({
    el: '#app',
    router
});

resources/assets/js/bootstrap.js

import Vue from 'vue';
import VueRouter from 'vue-router';
import axios from 'axios';

window.Vue = Vue;
Vue.use(VueRouter);

window.axios = axios;

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

[Edit 2]

resources/assets/js/routes.js

import VueRouter from 'vue-router';


let routes = [
    {
        path: '/',
        component: require('./views/Home.vue')
    }
];

export default new VueRouter({
    routes
});

回答1:


Ah! Thanks to TheFallen's questions, I see my problem. Here's what my routes.js file should look like:

import VueRouter from 'vue-router';
import Home from './views/Home.vue';

let routes = [
    {
        path: '/',
        component: Home
    }
];

export default new VueRouter({
    routes
});


来源:https://stackoverflow.com/questions/45081484/vuejs-router-failed-to-mount-component-template-or-render-function-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!