首页 > 解决方案 > 一起使用 Laravel8 + Inertia/vue.js 时显示空白页面(仅显示“@routes”文本)

问题描述

这是我的 webpack.mix.js

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

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

mix.js('resources/js/app.js', 'public/js').vue()
    .postCss('resources/css/app.css', 'public/css', [
        require('postcss-import'),
        require('tailwindcss'),
    ])
    .webpackConfig(require('./webpack.config'));

if (mix.inProduction()) {
    mix.version();
}

这是我的 app.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 inertia>{{ config('app.name', 'Laravel') }}</title>

    <!-- Fonts -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

    <!-- Styles -->
    <link rel="stylesheet" href="{{ mix('css/app.css') }}">

    <!-- Scripts -->
    @routes
    <script src="{{ mix('js/app.js') }}" defer></script>
</head>

<body class="font-sans antialiased">
    @inertia

    @env('local')
    <script src="http://localhost:3000/browser-sync/browser-sync-client.js"></script>
    @endenv
</body>

</html>

这是我的 web.php

<?php

use App\Http\Controllers\GoogleAuthController;
use App\Http\Controllers\KakaoAuthController;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/asdasd', function () {
    return Inertia::render('Newpop');
});


Route::get('/', function () {
    return view('welcome');
});

Route::post('/', function () {
    return view('welcome');
});


Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');


Route::get('/kakao/login', [KakaoAuthController::class, 'redirect'])->name('kakao.login');

Route::get('/kakao/callback', [KakaoAuthController::class, 'callback']);

Route::get('/github/login', [GithubAuthController::class, 'redirect'])->name('github.login');

Route::get('/github/callback', [GithubAuthController::class, 'callback']);


Route::get('/google/login', [GoogleAuthController::class, 'redirect'])->name('google.login');


Route::get('/google/callback', [GoogleAuthController::class, 'callback']);

这是我的 Newpop.vue(它在 js/Pages 文件夹中)

<template>
  <div>asdasddsdadzz</div>
</template>
<script>
export default {};
</script>

这是我的 app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue').default;

import {
    InertiaApp
} from '@inertiajs/inertia-vue'

Vue.use(InertiaApp)



new Vue({
    render: h => h(InertiaApp, {
        props: {
            initialPage: JSON.parse(app.dataset.page),
            resolveComponent: name => import(`./Pages/${name}`).then(module => module.default),
        },
    }),
}).$mount(app)
/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

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

Vue.component('example-component', require('./components/ExampleComponent.vue').default);
Vue.component('example-two', require('./components/ExampleTwo.vue').default);
Vue.component('hu-hu', require('./components/HuHu.vue').default);

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

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


// require('./bootstrap');

import Alpine from 'alpinejs';

window.Alpine = Alpine;

Alpine.start();

import "tailwindcss/tailwind.css"
import {
    InertiaProgress
} from '@inertiajs/progress'

createInertiaApp({
    resolve: name => import(`./Pages/${name}`),
    setup({
        el,
        App,
        props
    }) {
        new Vue({
            render: h => h(App, props),
        }).$mount(el)
    },
})

InertiaProgress.init()

在此处输入图像描述

当我启动 php artisan serve 时,

我的页面只显示@routes。我已经更改了 app.js 或 webpack.mix.js 之类的惯性文档,但它不起作用。

我的其他 vue 页面(不使用惯性,使用 js 前端脚手架)效果很好。

如何在我的项目中使用惯性组件?

在此处输入图像描述

标签: laravelvue.jsinertiajsjetstream

解决方案


推荐阅读