我想在Vue 3项目中使用Bootstrap 5。由于Bootstrap 5使用vanilla JS(不使用JQuery),所以我可以直接在Vue 3项目中使用Bootstrap 5吗(而无需使用Bootstrap-Vue)?能否有人指导我如何在Vue 3中使用Bootstrap 5?
我想在Vue 3项目中使用Bootstrap 5。由于Bootstrap 5使用vanilla JS(不使用JQuery),所以我可以直接在Vue 3项目中使用Bootstrap 5吗(而无需使用Bootstrap-Vue)?能否有人指导我如何在Vue 3中使用Bootstrap 5?
Bootstrap 5 不再需要 jQuery,因此与 Vue 结合使用更加容易,也不再需要像 bootstrap-vue 这样的库。
使用 npm install 或将其添加到 package.json
中,可以像在 Vue 项目中安装任何其他 JS 模块一样安装 Bootstrap...
npm install --save bootstrap
npm install --save @popperjs/core
接下来,将Bootstrap CSS和JS组件添加到Vue项目的入口点(即:src/main.js
)中...
import "bootstrap/dist/css/bootstrap.min.css"
import "bootstrap"
那么,使用Bootstrap组件最简单的方法是通过data-bs-
属性。例如,这里是Bootstrap Collapse组件...
<button
class="btn btn-primary"
data-bs-target="#collapseTarget"
data-bs-toggle="collapse">
Bootstrap collapse
</button>
<div class="collapse py-2" id="collapseTarget">
This is the toggle-able content!
</div>
或者,您可以导入任何Bootstrap组件并将它们“包装”为Vue组件。例如,这里是Popover组件...
import { Popover } from bootstrap;
const popover = Vue.component('bsPopover', {
template: `
<slot/>
`,
props: {
content: {
required: false,
default: '',
},
title: {
default: 'My Popover',
},
trigger: {
default: 'click',
},
delay: {
default: 0,
},
html: {
default: false,
},
},
mounted() {
// pass bootstrap popover options from props
var options = this.$props
var ele = this.$slots.default[0].elm
new Popover(ele,options)
},
})
<bs-popover
title="Hello Popover"
content="This is my content for the popover!"
trigger="hover">
<button class="btn btn-danger">
Hover for popover
</button>
</bs-popover>
npm i --save-dev @types/bootstrap
// https://www.npmjs.com/package/@types/bootstrap - Zeussi是的,你可以在没有Bootstrap-Vue的情况下使用Bootstrap。请使用npm安装这两个包:
npm install --save @popperjs/core bootstrap@next
在 src/main.js 中引入 Bootstrap:
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap";
Vue模板的示例用法:
<div class="dropdown">
<button
class="btn btn-secondary dropdown-toggle"
type="button"
id="dropdownMenuButton1"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Check Bootstrap
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
结果:
npm ERR! notarget No matching version found for bootstrap@next.
的错误信息。 - rubebop虽然Bootstrap CSS可以与任何框架(React、Vue、Angular等)一起使用,但Bootstrap JavaScript与它们不完全兼容。
以下是Bootstrap 5文档的官方解释:
尽管Bootstrap CSS可以与任何框架一起使用,但Bootstrap JavaScript与假定完全了解DOM的JavaScript框架(如React、Vue和Angular)不完全兼容。 Bootstrap和框架都可能试图改变相同的DOM元素,导致下拉菜单卡在“打开”位置的错误等问题。
文档指出,应该使用替代框架特定包而不是Bootstrap JavaScript,例如React Bootstrap、BootstrapVue和ng-bootstrap。
不幸的是,BootstrapVue只与Vue2/Nuxt2兼容,目前没有适用于Vue3/Nuxt3的版本。
<template>
<div>
<p>Test modal<a href="#" @click="modalToggle">now</a></p>
<div>
<button type="button" class="btn btn-primary" @click="modalToggle">My Modal</button>
<div
ref="modal"
class="modal fade"
:class="{ show: active, 'd-block': active }"
tabindex="-1"
role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
@click="modalToggle">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
</div>
</div>
</div>
<div v-if="active" class="modal-backdrop fade show"></div>
</div>
</div>
</template>
在这里,我们使用基本的Bootstrap 5模态框。
<script>
export default {
data() {
return {
active: false,
}
},
methods: {
modalToggle() {
const body = document.querySelector("body")
this.active = !this.active
this.active ? body.classList.add("modal-open") : body.classList.remove("modal-open")
},
},
}
</script>
这里,我们有一个名为active
的变量,最初设置为false。因此,模态框在页面加载时不会显示。单击链接时,我们使用一种方法来切换此变量。这将从我们的模态框中删除show属性和d-block类,并从body标签中删除modal-open
属性。
Bootstrap 5必须使用Popper才能运行,可以尝试使用以下npm:
npm install --save bootstrap
npm i @popperjs/core
<a
class="nav-link collapsed"
href="#"
data-toggle="collapse"
data-target="#collapseTwo"
aria-expanded="true"
aria-controls="collapseTwo"
>
<i class="fas fa-fw fa-cog"></i>
<span>Components</span>
</a>
<div
id="collapseTwo"
class="collapse"
aria-labelledby="headingTwo"
data-parent="#accordionSidebar"
>
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">Custom Components:</h6>
<a class="collapse-item" href="buttons.html">Buttons</a>
<a class="collapse-item" href="cards.html">Cards</a>
</div>
</div>
如果现有的data-属性之前引用了bootstrap功能,请将-bs添加到现有的data-属性中。请勿仅使用Cntrl-F和替换。
以下是上述示例的结果。
<a
class="nav-link collapsed"
href="#"
data-bs-toggle="collapse"
data-bs-target="#collapseTwo"
aria-expanded="true"
aria-controls="collapseTwo"
>
<i class="fas fa-fw fa-cog"></i>
<span>Components</span>
</a>
<div
id="collapseTwo"
class="collapse"
aria-labelledby="headingTwo"
data-parent="#accordionSidebar"
>
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">Custom Components:</h6>
<a class="collapse-item" href="buttons.html">Buttons</a>
<a class="collapse-item" href="cards.html">Cards</a>
</div>
</div>
编辑:以上假设您已经按照此答案操作
为了让Bootstrap与SSR配合使用,您不能:
import "bootstrap";
正如其他人所建议的,因为它会给你一个错误:
document is not defined
这不是最优解决方案,但它可以工作
npm install bootstrap
<style lang="scss">
@import 'bootstrap/scss/bootstrap';
.sticky-sidebar {
z-index: $zindex-sticky;
...
}
</style>
然后只需将Bootstrap 包添加到您的头文件即可。 注意:由于已在组件中导入CSS,因此现在无需添加CSS。