如何在Nuxt.js中集成Bootstrap 5?

huangapple go评论63阅读模式
英文:

How to integrate bootstrap 5 in nuxtjs?

问题

首先,我尝试集成bootstrap-vue,但它支持bootstrap 4,而且还支持vue2。所以我正在尝试集成bootstrap 5,按照以下步骤进行,不做任何更改:

  1. 我安装了bootstrap包(通过npm)。
  2. 然后我将编辑nuxt.config.ts文件,将css文件添加到css数组中,并将脚本添加到插件入口。

这是最终结果:

export default defineNuxtConfig({
   plugins: [
     {
       src: "~/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",
       mode: "clients",
     },
   ],
   css: [
     "~/node_modules/bootstrap/dist/css/bootstrap.min.css",
     "@/assets/css/global.css",
   ],
});

现在css正常工作,问题仍然存在于js文件中。我不明白为什么,但没有任何动画效果都无法正常工作,从下拉菜单到轮播图都有问题。

英文:

First, I tried integrating bootstrap-vue, but that supports bootstrap 4, and worse vue2.
So I'm trying to integrate bootstrap 5 without any changes by following these steps:

  1. I install the bootstrap package (via npm).
  2. So I'm going to edit the nuxt.config.ts file, adding the css files in the css array and the script in the plugin entry.

This is the final result:

export default defineNuxtConfig({
   plugins: [
     {
       src: "~/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",
       mode: "clients",
     },
   ],
   css: [
     "~/node_modules/bootstrap/dist/css/bootstrap.min.css",
     "@/assets/css/global.css",
   ],
});

Now the css works fine, the problem remains with the js file.
I don't understand why, but nothing animated works, starting from dropdowns, to carousels.

答案1

得分: 1

I followed some older StackOverflow answers and managed (I think) to create working demo with newest Nuxt 3 and Bootstrap 5 like this:

https://stackblitz.com/edit/github-bebg1r-j6lqxm

The key things:

  1. In package.json add and install dependencies for bootstrap, sass, and sass-loader
  2. Create .scss file in /assets folder containing @import 'bootstrap/scss/bootstrap';
  3. Load this file in nuxt.config.ts via css: ['~/assets/main.scss']
  4. Plugin to load JS bundle in /plugins/bootstrap.client.ts
英文:

I followed some older StackOverflow answers and managed (I think) to create working demo with newest Nuxt 3 and Bootstrap 5 like this:

https://stackblitz.com/edit/github-bebg1r-j6lqxm

The key things:

  1. In package.json add and install dependencies for bootstrap, sass and sass-loader
  2. Create .scss file in /assets folder containg @import 'bootstrap/scss/bootstrap';
  3. Load this file in nuxt.config.ts via css: ['~/assets/main.scss']
  4. Plugin to load JS bundle in /plugins/bootstrap.client.ts

huangapple
  • 本文由 发表于 2023年5月10日 20:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定