英文:
How to use and initialize bootstrap5 tooltip in nuxt (data-bs-toggle="tooltip" )?
问题
The code you provided is meant to initialize Bootstrap tooltips in a Nuxt.js project. However, it seems like there might be an issue with the jQuery dependency. To make it work with Bootstrap 5 and Nuxt.js, you can modify the code like this:
import Vue from 'vue';
import 'bootstrap/dist/js/bootstrap.bundle.min.js'; // Include Bootstrap JavaScript
const bsTooltip = (el, binding) => {
const t = [];
if (binding.modifiers.focus) t.push('focus');
if (binding.modifiers.hover) t.push('hover');
if (binding.modifiers.click) t.push('click');
if (!t.length) t.push('hover');
// Use vanilla JavaScript to initialize tooltips
new bootstrap.Tooltip(el, {
title: binding.value,
placement: binding.arg || 'top',
trigger: t.join(' '),
html: !!binding.modifiers.html,
});
};
Vue.directive('tooltip', {
bind: bsTooltip,
update: bsTooltip,
unbind(el) {
// Dispose of the tooltip when the element is unbound
const tooltip = bootstrap.Tooltip.getInstance(el);
if (tooltip) {
tooltip.dispose();
}
},
});
Make sure you've imported Bootstrap's JavaScript (bootstrap.bundle.min.js
) properly in your project. This code should work with Bootstrap 5 and Nuxt.js.
英文:
Bootstrap 5 documentation says I have to add this code to initialize tooltips.
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
How should this be done properly in nuxt so the tooltips are initialized and can be used globally?
the nearest thing I found was in an answer to this question: https://stackoverflow.com/questions/37078423/how-can-add-bootstrap-tooltip-inside-vue-js
where it states to do by creating a bs-tooltips.js file in the plugin folder with the following code:
import Vue from 'vue'
const bsTooltip = (el, binding) => {
const t = []
if (binding.modifiers.focus) t.push('focus')
if (binding.modifiers.hover) t.push('hover')
if (binding.modifiers.click) t.push('click')
if (!t.length) t.push('hover')
$(el).tooltip({
title: binding.value,
placement: binding.arg || 'top',
trigger: t.join(' '),
html: !!binding.modifiers.html,
});
}
Vue.directive('tooltip', {
bind: bsTooltip,
update: bsTooltip,
unbind (el) {
$(el).tooltip('dispose')
}
});
then add this to the nuxt config file:
plugins: [
'~/plugins/bs-tooltips.js'
],
and then this should work:
<button v-tooltip="'Tooltip text'">Hover me</button>
<button v-tooltip.click="Tooltip text">Click me</button>
<button v-tooltip.html="Tooltip text">Html</button>
<button v-tooltip:bottom="Tooltip text">Bottom</button>
<button v-tooltip:auto="Tooltip text">Auto</button>
but that throws following ERROR: $ is not defined
My guess is that this is probably meant for bootstrap 4 / jQuery and probably an older version of nuxt (the answer is from 2018)
Has anyone integrated this feature and bootstrap 5 in nuxt 2?
Is interesting if it works as a directive of course, but I'm inclined to just have it work as the documentation of bootstrap states, with data-bs-toggle="tooltip"
and data-bs-title
attributes
答案1
得分: 0
以下是翻译好的部分:
老的插件集成方法有效,但需要以下代码:
我在我的插件文件夹中创建了 "bs-tooltips.js" 文件,然后将以下代码放入其中:
export default function ({ app }) {
if (process.client) {
window.onload = () => {
const tooltips = document.querySelectorAll('[data-bs-toggle="tooltip"]');
tooltips.forEach((tooltip) => {
if (!tooltip._tooltip) {
new window.bootstrap.Tooltip(tooltip);
}
});
};
}
}
然后将这段代码添加到 Nuxt 配置文件中:
plugins: [
'~/plugins/bs-tooltips.js'
],
之后,下面的代码应该按预期工作:
<a href="#" data-bs-toggle="tooltip" data-bs-title="默认工具提示">内联链接</a>
注意: 请确保在您的 Nuxt.js 应用程序中正确集成了 Bootstrap 本身,使用了 bootstrap.bundle.min.js 或 popper,并且所有下拉菜单和常用的 Bootstrap 组件都已按预期工作。
英文:
the old approach of integrating as a plugin works, but with the following code:
I created the bs-tooltips.js
file in my plugins folder, then put the following code into that:
export default function ({ app }) {
if (process.client) {
window.onload = () => {
const tooltips = document.querySelectorAll('[data-bs-toggle="tooltip"]');
tooltips.forEach((tooltip) => {
if (!tooltip._tooltip) {
new window.bootstrap.Tooltip(tooltip);
}
});
};
}
}
then add this to the nuxt config file:
plugins: [
'~/plugins/bs-tooltips.js'
],
after that:
<a href="#" data-bs-toggle="tooltip" data-bs-title="Default tooltip">inline links</a>
should work as expected
NOTE: Be Sure that bootstrap itself with the bootrap.bundle.min.js or popper is properly integrated into your nuxt.js application and all dropdowns and useal bootstrap components are already working as expected
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论