npm 当尝试使用Bootstrap 5与Laravel时,没有定义bootstrap

5cnsuln7  于 4个月前  发布在  Bootstrap
关注(0)|答案(1)|浏览(51)

这是一个常见的问题,但到目前为止还没有解决方案.我使用Laravel 10与Vite,安装Bootstrap与NPM和配置是确定使用引导CSS和引导图标,但我不能加载引导JS,这是必要的,使其工作模态,烤面包,弹出窗口,等.
vite.config.js:

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
    resolve: {
        alias: {
            '~bootstrap': path.resolve(__dirname, "node_modules/bootstrap"),
            '~bootstrap-icons': path.resolve(__dirname, "node_modules/bootstrap-icons/font")
        }
    }
});

字符串
resources/js/app.js

import * as bootstrap from 'bootstrap';


在主blade.php布局中删除文件(所有其他页面都扩展到该布局):

...
@vite(['resources/css/app.css', 'resources/js/app.js'])
...


以及发生错误的blade.php页面(未定义 Bootstrap )

...
<script>
    window.onload = () => {
        const toastLiveExample = document.querySelector("#liveToast")
        const toast = new bootstrap.Toast(toastLiveExample)
        toast.show()
    }
</script>
...

r8uurelv

r8uurelv1#

app.js文件中。您可以定义window.bootstrap = bootstrap,以便可以在刀片中使用它。

import * as bootstrap from 'bootstrap';
window.bootstrap = bootstrap;

字符串

相关问题