简体   繁体   中英

<router-view> in laravel 5.6 and vue.js don't work

I do not know what I'm doing wrong, but my router-view does not work.

I have an app based on Laravel 5.6 and I want to make views through vue.js. Components "Navbar" and "Foot" are load correctly but I don't see "Home" component which should be load by in App.vue

Routing also does not work. When I type in the browser /about I get an error "Sorry, the page you are looking for could not be found."

Below my files:

App.vue

<template>
    <div class="app">
        <Navbar/>
        <router-view></router-view> 
        <Foot/>
    </div>
</template>

<script>
export default {
    name: 'App',
    data () {
        return{

        }
    }
}
</script>

Home.vue

<template>
    <div class="home">
        <h1>Home</h1>   
    </div>
</template>

<script>
export default {
    name: 'Home',
    data () {
        return {

        }
    }

}
</script>

About.vue

<template>
    <div class="about">
        <h1>O nas</h1>
    </div>
</template>

<script>
export default {
    name: 'About',
    data (){
        return{

        }
    }
}
</script>

app.js

require('./bootstrap');
import Vue from 'vue';
import VueRouter from 'vue-router';
import Vuex from 'vuex';

window.Vue = require('vue'); 

Vue.use(VueRouter);
Vue.use(Vuex);

let AppLayout = require('./components/App.vue');

// home tempalte
const Home = Vue.component('Home', require('./components/Home.vue'));
// About tempalte
const About = Vue.component('About', require('./components/About.vue'));

// register components
const Navbar = Vue.component('Navbar', require('./components/Navbar.vue'));
const Foot = Vue.component('Foot', require('./components/Foot.vue'));

const routes = [
    {
        path: '/',
        name: 'Home',        
        component: Home
    },
    {

        name: 'About', 
        path: '/about',   
        component: About
    }
];

const router = new VueRouter({ mode: 'history', routes: routes});

new Vue(
    Vue.util.extend(
        { router },
        AppLayout
    )
).$mount('#app');

You need to teach laravel's router how to play along with vue's.
Check out this link: https://medium.com/@piethein/how-to-combine-vuejs-router-with-laravel-1226acd73ab0

You need to read around where it says:

Route::get('/vue/{vue_capture?}', function () {
 return view('vue.index');
})->where('vue_capture', '[\/\w\.-]*');

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