2

I am following the following this guide, that shows how to enable "Route Based Modals" in Inertia.js.

The post is written for Vue2, and I am using Vue3 - and I am having some issues getting it to work.

This is my "Composable" for my "useModal" method:

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

export { useModal }

I then declare it in my app.js file as per below:

//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);
    },
});

In my main layout, called AppLayout I have declared the Component:

<body>

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

</body>

Further, I have declared the modal() method in my AppServiceProvider:

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

Now, I am trying to render a Vue component in a modal like so:

//FileController.php

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

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

When linking to the modal component, the following Warning is issued:

[Vue warn]: Invalid VNode type: undefined (undefined) at <Anonymous key=0 ...

1 Answer 1

1

I ended up fixing this by using defineAsyncComponent instead:

//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 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.