Laravel:vue 组件不渲染

Bog*_*dan 4 php laravel vue.js

尽管遵循了以下教程,但我的 vue 组件未在页面上呈现。我有以下布局(master.blade.php):

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Mileage</title>
    <link rel="stylesheet" href="{{ mix('/css/app.css') }}" media="all">
</head>

<body>
    <div id="app">
        <example-component></example-component>
    </div>
    <script src="{{ mix('/js/app.js') }}"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

布局由以下控制器返回(我使用 CAS 登录):

<?php

namespace App\Http\Controllers;

use App\LkpStaff;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function login()
    {
        if (!cas()->isAuthenticated())
        {
            cas()->authenticate();
        }

        $user = cas()->user();

        $dbUser = LkpStaff::where('user_name', $user)->first(['staff_refid']);

        if ($dbUser)
        {
            $userId = $dbUser->staff_refid;
        }else
        {
            $userId = $this->createNewUser($user);
        }

        session()->put('casUser', $user);
        session()->put('userId', $userId);

        return view('master', [
            'casUser' => $user,
            'userId' => $userId
        ]);
    }
}
Run Code Online (Sandbox Code Playgroud)

CAS 登录按预期工作,所以这不是问题。我只是想渲染 laravel 附带的示例组件(ExampleComponent.vue):

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Example Component</div>

                    <div class="card-body">
                        I'm an example component.
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我的 app.js 文件按预期注册了组件:

require('./bootstrap');

window.Vue = require('vue');


Vue.component('example-component', require('./components/ExampleComponent.vue'));

const app = new Vue({
    el: '#app'
});
Run Code Online (Sandbox Code Playgroud)

我的 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.18",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.5",
        "popper.js": "^1.12",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue": "^2.5.17",
        "vue-template-compiler": "^2.6.10"
    },
    "dependencies": {
        "bulma": "^0.7.0",
        "font-awesome": "^4.7.0",
        "laravel-blade-compiler": "^1.0.10",
        "moment": "^2.24.0",
        "purify-css": "^1.2.5"
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用了 npm install 和 npm run dev。我使用 php artisan serve 为该项目提供服务。该页面如下所示(在浏览器控制台中):

<html><head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="csrf-token" content="gLzt6ZloAyL7kFEVuO0tHlv6XqlsCMlELSx9Eqg6">
    <title>Mileage</title>
    <link rel="stylesheet" href="/css/app.css" media="all">
</head>

<body>
    <div id="app">
        <example-component></example-component>
    </div>
    <script src="/js/app.js"></script>

</body></html>
Run Code Online (Sandbox Code Playgroud)

如您所见,组件没有被渲染。控制台中也没有错误。除了创建一个新项目,我还能做些什么?

小智 6

也许你的分量不是由于渲染错误的道路问题app.cssapp.js

尝试使用以下资产进行渲染:

<link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css">

<script src="{{asset('js/app.js')}}"></script>