Michael
Michael

Reputation: 375

Laravel 6.1 not loading Vue

I just did a fresh install of Laravel 6.1 and ran

composer require laravel/ui
php artisan ui vue
npm install && npm run dev

But when looking at my page in the browser I see that the Vue component is not displayed and Vue isn't even loaded at all. I have the following errors in my DOM.

enter image description here

Are these the reason that Vue isn't loading? And how do I solve them? The url should be http://localhost/laravel_applications/webgame/public/css/app.css and http://localhost/laravel_applications/webgame/public/js/app.js how can I change this? I'm running it on Xammp and it's in the htdocs folder under /laravel_applications/webgame. I tried changing the APP_URL in the env file to

APP_URL=http://localhost/laravel_applications/webgame/public

but that did not work.

Vue is installed in my project and my app.js looks like this

require('./bootstrap');

window.Vue = require('vue');
import Vue from 'vue';

// const files = require.context('./', true, /\.vue$/i);
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));

Vue.component('application', require('./components/application.vue').default);

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

I have an components/application.vue that looks like this

<template>
  <div>
      Test
  </div>
</template>

<script>
export default {

}
</script>

<style>

</style>

And a welcome.blade.php that looks like this

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>SPA</title>
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
        <link rel="stylesheet" href="{{ mix('css/app.css') }}" />
        <script defer src="{{ mix('js/app.js') }}"></script>
    </head>
    <body>
        <div id="app">
            <application></application>
        </div>
    </body>
</html>

My package.json looks like this

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.19",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.13",
        "popper.js": "^1.12",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.20.1",
        "sass-loader": "7.*",
        "vue": "^2.5.17",
        "vue-template-compiler": "^2.6.10"
    },
    "dependencies": {
        "vue-router": "^3.1.3"
    }
}

And this is my mixin file

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css');

Upvotes: 2

Views: 985

Answers (1)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

you don't need to put

APP_URL=http://localhost/laravel_applications/webgame/public

laravel has helper asset() which is point to public folder of laravel so anything inside you public dir you can load via asset() function

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>SPA</title>
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
        <link rel="stylesheet" href="{{ asset('css/app.css') }}" />
        <script defer src="{{ asset('js/app.js') }}"></script>
    </head>
    <body>
        <div id="app">
            <application></application>
        </div>
    </body>
</html>

ref link https://laravel.com/docs/master/helpers#method-asset

Upvotes: 2

Related Questions