4

我正在尝试设置 Inertia 以在我的 Laravel 项目中使用,但它给了我错误?我的错误在哪里?

我用这个命令安装了惯性 composer require inertiajs/inertia-laravel

按照 github 页面上的说明添加@inertia到我的 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">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>
    <link rel="icon" type="image/jpg" href="{{asset("/image/logo2.png")}}">
    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

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

@inertia

</body>
</html>

在我的登录控制器中

 public function showLoginForm()
    {
        return Inertia::render('Auth/Login');
    }

在我的路线/web.php

Auth::routes();
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');

这是我得到的错误:

突出显示的行是@inertia这样显示的
<div id="app" data-page="<?php echo e(json_encode($page)); ?>"></div>

我究竟做错了什么?

4

1 回答 1

4

@inertia刀片指令正在工作但未呈现,因为您需要安装前端适配器

npm install @inertiajs/inertia @inertiajs/inertia-vue

设置在webpack.mix.js 

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

mix.js('resources/js/app.js', 'public/js')
  .webpackConfig({
    output: { chunkFilename: 'js/[name].js?id=[chunkhash]' },
    resolve: {
      alias: {
        vue$: 'vue/dist/vue.runtime.esm.js',
        '@': path.resolve('resources/js'),
      },
    },
  })

并在Vue中初始化resources/js/app.js

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

Vue.use(InertiaApp)

const app = document.getElementById('app')

const pages = {
  'Auth/Login': require('./Pages/Auth/Login.vue').default,
}

new Vue({
  render: h => h(InertiaApp, {
    props: {
      initialPage: JSON.parse(app.dataset.page),
      resolveComponent: name => pages[name],
    },
  }),
}).$mount(app)
于 2019-09-27T15:48:59.457 回答