Laravel 6.1未加载Vue

时间:2020-01-13 04:13:51

标签: laravel

我刚刚重新安装了Laravel 6.1并运行了

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

但是在浏览器中查看我的页面时,我发现Vue组件没有显示,甚至根本没有加载Vue。我的DOM中出现以下错误。

enter image description here

这些是Vue无法加载的原因吗?我该如何解决呢?网址应为http://localhost/laravel_applications/webgame/public/css/app.csshttp://localhost/laravel_applications/webgame/public/js/app.js,我该如何更改?我正在Xammp上运行它,它在/laravel_applications/webgame下的htdocs文件夹中。我尝试将环境文件中的APP_URL更改为

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

但这没用。

Vue已安装在我的项目中,我的app.js看起来像这样

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

我有一个组件/应用程序。vue看起来像这样

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

<script>
export default {

}
</script>

<style>

</style>

还有一个看起来像这样的welcome.blade.php

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

我的package.json看起来像这样

{
    "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"
    }
}

这是我的混入文件

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

1 个答案:

答案 0 :(得分:2)

您不需要放

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

laravel有帮助器asset(),它指向laravel的public文件夹,因此您可以通过asset()函数加载公共目录中的任何内容

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

参考链接https://laravel.com/docs/master/helpers#method-asset