繁体   English   中英

如何在 Laravel 8 模块中使用 InertiaJS 加载 Vue 组件

[英]How to load Vue Component with InertiaJS in Laravel 8 Modules

使用Laravel 模块

在 Laravel 8 中我们可以使用Inertiajs加载组件

我们用@inertiaapp.blade.php中初始化Inertia

之后在app.js文件中,我们可以像这样初始化它:

const app = document.getElementById('app');

new Vue({
    render: (h) =>
        h(InertiaApp, {
            props: {
                initialPage: JSON.parse(app.dataset.page),
                resolveComponent: (name) => require(`./Pages/${name}`).default,
            },
        }),
}).$mount(app);

然后所有组件将从Pages文件夹加载。

现在,我的问题是:

我怎样才能在模块中实现它? 我想使用 Inertiajs 加载 Vuejs 组件。 在模块中,这个Resources/assets/js/app.js文件是空的。 如果我将上面的代码放入其中并创建一个 Pages 文件夹,但它会给我错误!

import { createApp, h } from 'vue'
import { App, plugin } from '@inertiajs/inertia-vue3'

const el = document.getElementById('app')

createApp({
  render: () => h(App, {
    initialPage: JSON.parse(el.dataset.page),
    resolveComponent: name => require(`./Pages/${name}`).default,
  })
}).use(plugin).mount(el)

在核心(我做了这个)模块master.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Module Core</title>
       <link rel="stylesheet" href="{{ mix('css/core.css') }}">

    </head>
    <body>
        @inertia
        <script src="{{ mix('js/core.js') }}"></script>
    </body>
</html>

核心控制器:

use Inertia\Inertia;

class CoreController extends Controller
{
    public function index()
    {
        return Inertia::render('Admin/Index');
    }
}

我在assets/js/Pages/Admin/Index.vue中有一个页面

我想加载这个。 但是给我错误:

[Vue 警告]:创建挂钩时出错:“错误:找不到模块‘./Admin/Index’”

检查您的 Index vue 页面是否在此目录中:resources/js/Page/Admin 如果是,则复制您的 Index vue 页面中的所有内容并删除 Index vue 文件。 再次重新创建文件,但这次确保您输入了带有 .vue 扩展名的文件,然后重试

Laravel 不再使用js文件夹中的assets文件夹。

所以正确的路径应该是resources/js/app.js而不是Resources/assets/js/app.js

要做出惯性响应,请使用惯性渲染函数。 此方法采用组件名称,并允许您传递道具和查看数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM