简体   繁体   中英

Not loading vue file in Laravel 5.7 application

working with laravel 5.7 and vue.js my app.js file is as following,

require('./bootstrap');
window.Vue = require('vue');
import VueRouter from 'vue-router'
Vue.use(VueRouter)

let routes = [
    { path: '/dashboard', component: require('./components/Dashboard.vue') },
    { path: '/profile', component: require('./components/Profile.vue') }
  ]

  const router = new VueRouter({
    routes // short for `routes: routes`
  })
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
const app = new Vue({
    el: '#app',
    router
});

and I need link following link with vue file

<router-link to="/dashboard" class="nav-link">

and Dashboard.vue file is like this

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card card-default">
                    <div class="card-header">Dashboard Component</div>

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

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

but when I click above link to dashbord.vue file it is not loading. only display url in the address bar. my console error is as following [Vue warn]: Failed to mount component: template or render function not defined. found in ---> <Anonymous> <Root> [Vue warn]: Failed to mount component: template or render function not defined. found in ---> <Anonymous> <Root>

how can fix this error

You need add App.vue

<template>
  <div>
    <router-link to="/dashboard" class="nav-link">Dashboard</router-link>
  </div>
</template>
<script>
  export default {
    // some code here if needed
  }
</script>

and then use it in main.js

require('./bootstrap'); // maybe better use import "./bootstrap"

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

import App from "./App.vue";
import Dashboard from './components/Dashboard.vue';
import Profile from './components/Profile.vue';
import ExampleComponent from './components/ExampleComponent.vue';

Vue.use(VueRouter)

window.Vue = Vue; // why you do that???

let routes = [
    { path: '/dashboard', component: Dashboard },
    { path: '/profile', component: Profile }
  ]

  const router = new VueRouter({
    routes // short for `routes: routes`
  });

Vue.component('example-component', ExampleComponent);
new Vue({
    router,
    render: (h) => h(App)
}).$mount('#app');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM