繁体   English   中英

在 Vue3 (Inertia.js) 中实现基于路线的模型

[英]Implementing route based models in Vue3 (Inertia.js)

我遵循以下本指南,它展示了如何在 Inertia.js 中启用“ 基于路由的模式”。

这篇文章是为 Vue2 编写的,我正在使用 Vue3 - 我在使用它时遇到了一些问题。

这是我的“useModal”方法的“可组合”:

//Composables/useModal.js
const useModal = {
    computed: {
        modalComponent() {
            return this.$page.props.modal
                ? () => import(`@/Pages/${this.$page.props.modal}`)
                : false
        }
    }
}

export { useModal }

然后我在我的app.js文件中声明它如下:

//app.js
//...

import {useModal} from "@/Composables/useModal";

createInertiaApp({
    title: (title) => `${title}`,
    resolve: (name) => require(`./Pages/${name}.vue`),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .component("Link", Link)
            .mixin(useModal)
            .mixin({ methods: { route } })
            .mount(el);
    },
});

在我名为AppLayout的主布局中,我声明了Component

<body>

<!-- Main elements are here, removed for clarity -->
<Component
        v-bind="$page.props"
        v-if="$root.modalComponent"
        :is="$root.modalComponent"
    />

</body>

此外,我在AppServiceProvider中声明了modal()方法:

public function boot()
{
    ResponseFactory::macro('modal', function ($modal) {
        inertia()->share(['modal' => $modal]);
    });
}

现在,我正尝试在模态中渲染一个 Vue 组件,如下所示:

//FileController.php

public function show(File $file){
   \inertia()->modal('File/Show');

   return $this->index($file);
}

链接到模态组件时,发出以下警告:

[Vue 警告]: 无效的 VNode 类型: 未定义 (undefined) at <Anonymous key=0...

我最终通过使用defineAsyncComponent解决了这个问题:

//Composables/useModal.js
import {defineAsyncComponent} from "vue";

const useModal = {
    computed: {
        modalComponent() {
            return this.$page.props.modal
             ? defineAsyncComponent(() => import(`@/Pages/${this.$page.props.modal}`))
                : false
        }
    }
}

export { useModal }

暂无
暂无

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

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