1

I am trying to remove any blade templates from my Laravel project, so this includes the homepage. I have left myself app.blade.php which contains the following

<!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>

So a Navigation Component and a router-view. I have installed the router package. Within web.php I have:

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

Navigation.vue is simple:

<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>

I also have a couple of basic components: App.vue and Dashboard.vue that contain basic templates. And then in app.js I have:

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
});

So when I visit my localhost:8000 I see a mostly empty page besides the navigation, as to be expected. However, when I click on the link, the content of a sample component is not being displayed, even though the url is changing. For example, Dashboard component is:

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

Is there any reason within my setup why it is not displaying the content by clicking the navigation?

1 Answer 1

2

Using require(...).default is required by vue-loader@15 which included in laravel-mix@4. If you're using previous versions of laravel-mix (as it seems) you should not add .default:

let routes = [
    { path: '/', component: require('./components/App.vue') },
    { path: '/dashboard', component: require('./components/Dashboard.vue') }
];
Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks, that seems to work. Why does this work? A lot of the tutorials I have followed use the .default
@kate_hudson I think you're confusing it with dynamic imports: import(...).default
@kate_hudson It also depends on laravel-mix version you're using. Prior to 4.0 it was require(), but since 4.0 you should use require().default. laravel-mix.com/docs/4.0/upgrade#importing-es-modules
Ah, thanks. I was working with the latest, then downgraded to Laravel 5.5 because there is no stable release of PHP 7.1 for our server, that explains it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.