使用 Vue 3 在 InertiaJS 中定义全局组件

问题描述 投票:0回答:3

我正在尝试在

Laravel 8.38 - InertiaJS 0.8.4
上构建应用程序,我使用
Vue 3
作为我的前端堆栈。

我有多个布局,需要将其全局注册为

Vue
组件,以便我可以在应用程序中的任何地方使用它。我无法这样做,我的代码:

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

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

const app = createApp({
    render: () =>
        h(InertiaApp, {
            initialPage: JSON.parse(el.dataset.page),
            resolveComponent: (name) => require(`./../Pages/${name}`).default,
        }),
})
    .mixin({ methods: { route } })
    .use(InertiaPlugin);

app.component('app-market', () => import('./../layouts/AppMarketLayout')); //trying to import layout

app.mount(el);

在页面内我尝试调用此布局组件:

<template>
    <div>
        <app-market-layout></app-market-layout>
    </div>
</template>

无法获取,控制台中没有错误。

laravel vue.js vuejs3 inertiajs
3个回答
2
投票

在Inertia JS中,全局导入组件与使用普通的Vue配置相同。

在 app.js 文件中,您需要导入组件(内联或在文件顶部),然后在挂载 vue 实例之前添加该组件。 您可以将其作为 mixin 或组件来执行。

因此,在您的情况下,配置应如下所示:

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

//Import the component:
import AppMarket from "./../layouts/AppMarketLayout"; 

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

const app = createApp({
    render: () =>
        h(InertiaApp, {
            initialPage: JSON.parse(el.dataset.page),
            resolveComponent: (name) => require(`./../Pages/${name}`).default,
        }),
})
    .mixin({ methods: { route } })
    .mixin({ components: { AppMarket } }) // << Then tell the inertia 
             // instance to use the component. 
             // You can also add this to the other mixin declaration
    .use(InertiaPlugin)
    .mount(el);

然后,在您的模板中,您可以像这样使用该组件:

<template>
    <app-market>
    </app-market>
</template>

我注意到在您原来的帖子中您正在尝试使用该组件作为应用程序市场布局。如果您打算这样做,则必须导入具有该名称的组件。

app.js 中的配置如下所示:

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

//Import the component: Notice the component name change.
import AppMarketLayout from "./../layouts/AppMarketLayout"; 

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

const app = createApp({
    render: () =>
        h(InertiaApp, {
            initialPage: JSON.parse(el.dataset.page),
            resolveComponent: (name) => require(`./../Pages/${name}`).default,
        }),
})
    .mixin({ methods: { route } })
    .mixin({ components: { AppMarketLayout } }) // << Here the component name has Layout
    .use(InertiaPlugin)
   
    .mount(el);

然后在您的模板中:

<template>
    <app-market-layout>
    </app-market-layout>
</template>

1
投票

我有 2 个后端(管理面板)和前端(网站)布局,实现如下。使用惯性默认中间件文件。

项目/应用程序/Http/Middleware/HandleInertiaRequests.php

class HandleInertiaRequests extends Middleware
{
    protected $rootView = 'app';

    public function rootView(Request $request)
    {
        if ($request->segment(1) == 'admin') {
            return 'admin';
        }

        return parent::rootView($request);
    }

    public function version(Request $request)
    {
        return parent::version($request);
    }

    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            //
        ]);
    }
}

项目/资源/js/共享/前端/布局/Layout.vue

<template>

    <Header />

    <slot />

    <Footer />

</template>

<script>
    import Header from '../Partials/Header'
    import Footer from '../Partials/Footer'
    export default {
        components: {
            Header,
            Footer
        },

        name: "FrontendLayout"
    }
</script>

项目/资源/js/Pages/Frontend/Auth/Login.vue

<template>
    ...
</template>

<script>
    import Layout from '@/js/Shared/Frontend/Layouts/Layout';
    export default {
        
        layout: Layout,

        metaInfo: { title: 'Login' },

        data() {
            return {
                form: this.$inertia.form({
                    'email': '',
                    'password': '',
                    'remember': false
                })
            }
        },

        methods: {
            submit() {
                this.form.post(route('login.store'))
            }
        }
    }
</script>

0
投票

在最新的 Inertia 中,只需导入 'Link' 组件并在 .use() 和 .mount() 之间添加 .component("Link", Link)

import { createApp, h } from 'vue';
import {createInertiaApp, Link} from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress'

createInertiaApp({
    resolve: name => {
        const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })
        return pages[`./Pages/${name}.vue`]
    },
    setup({ el, App, props, plugin }) {
        createApp({ render: () => h(App, props) })
            .use(plugin)
            .component("Link", Link)
            .mount(el)
    },
});
© www.soinside.com 2019 - 2024. All rights reserved.