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
相关产品推荐
相关产品推荐

