在整个Laravel应用中使用VueJS

时间:2019-01-17 14:39:02

标签: laravel vue.js

我正在尝试从Laravel项目中删除所有刀片服务器模板,因此包括主页。我离开了app.blade.php,其中包含以下内容

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>My Website</title>

    <link href="/css/app.css" rel="stylesheet">
</head>
<body>
<div id="app">
    <Navigation></Navigation>
    <div class="container">
        <main>
            <router-view></router-view>
        </main>
    </div>
</div>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>

因此,导航组件和路由器视图。我已经安装了路由器软件包。在web.php中,我有:

Route::get('/{any}', function(){
    return view('layouts.app');
})->where('any', '.*');

Navigation.vue 很简单:

<template>
    <div>
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header">
                    <router-link to="/" tag="a" class="nav-link" active-class="active" exact>
                        <i class="nav-icon fas fa-tachometer-alt"></i>
                        <p>
                            Website Name
                        </p>
                    </router-link>
                </div>
                <ul class="nav navbar-nav">
                    <router-link to="/dashboard" tag="a" class="nav-link" active-class="active" exact>
                        <i class="nav-icon fas fa-tachometer-alt"></i>
                        <p>
                            Dashboards
                        </p>
                    </router-link>
                </ul>
            </div>
        </nav>
    </div>
</template>
<script>
    export default {}
</script>

我也有几个基本组件:App.vueDashboard.vue包含基本模板。然后在app.js中,我有:

require('./bootstrap');

window.Vue = require('vue');

import VueRouter from 'vue-router';
Vue.use(VueRouter);

import Navigation from './components/Navigation';

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

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

const app = new Vue({
    el: '#app',
    components: { Navigation },
    router
});

因此,当我访问localhost:8000时,除了预期的导航之外,我还看到一个几乎是空白的页面。但是,当我单击链接时,即使URL更改了,也不会显示示例组件的内容。例如,Dashboard组件是:

<template>
    <div>
        <main>
            <h1>This is a dashboard</h1>
        </main>
    </div>
</template>
<script>
    export default {}
</script>

我的设置中是否有任何原因导致它不通过单击导航显示内容?

1 个答案:

答案 0 :(得分:2)

require(...).default要求vue-loader@15使用laravel-mix@4。如果您使用的是laravel-mix的早期版本(看起来),则不应添加.default

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