You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue3 + Inertia + Vite环境下全局引入第三方包(如SweetAlert2)失效的问题求助

Vue3 + Inertia + Vite环境下全局引入第三方包(如SweetAlert2)失效的问题求助

大家好,我目前正在用Laravel、Vue3、Vite和Inertia搭建项目,碰到了一个挺头疼的问题:

我试着在app.js里全局导入SweetAlert2这类第三方包,但在页面里直接调用Swal时会抛出错误:Uncaught (in promise) ReferenceError: Swal is not defined。只有在每个需要用到的Inertia页面(.vue文件)里单独导入这个包,才能正常使用功能,但这样每个页面都要重复导入,效率太低了,想请教下有没有办法实现真正的全局引入?

我的app.js代码如下:

import './bootstrap';
import { createApp, h } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3';
import Swal from 'sweetalert2';

createInertiaApp({
resolve: async name => {
    const pages = import.meta.glob('/Modules/**/resources/assets/js/Pages/**/*.vue', { eager: false });
    const matchingPageKey = Object.keys(pages).find(key => key.endsWith(`/${name}.vue`));

    let page;
    if (matchingPageKey) {
        const module = pages[matchingPageKey];
        // check if module = function then module is lazyload (eager = false) so it needs await
        page = typeof module === 'function' ? await module() : module;
    }

    return page;
},
setup({ el, App, props, plugin }) {
    const app = createApp({ render: () => h(App, props) });
    app.use(plugin);
    app.mount(el);
},
});

备注:内容来源于stack exchange,提问作者K1-Aria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:27:58