[英]Popover with vue.js and bootstrap 4
我正在嘗試將 bootstrap 4 中的彈出窗口添加到我的 vue.js 應用程序中。 我可能可以使用類似https://www.npmjs.com/package/vue-popperjs
東西,但我想讓它以舊方式工作,而不使用額外的庫(這樣我才能弄清楚如何這樣做)
我已經使用npm install --save popper
安裝了Popper.js
在我的webpack.config.js
我也有:
plugins: [
new WebpackNotifierPlugin(),
new webpack.ProvidePlugin({
_: 'lodash',
$: 'jquery',
jquery: 'jquery',
'window.jQuery': 'jquery',
jQuery: 'jquery',
popper: 'popper'
})
],
然后我正在嘗試構建一個 vue 組件:
<template>
....
<button type="button" class="btn btn-link" title="" data-container="body" data-toggle="popover" data-placement="bottom" data-original-title="Connection String" aria-describedby="popover253231">
Click to show
</button>
....
</template>
<script>
const _ = require("lodash");
const $ = require("jquery");
// This doesn't work
//const poppper = require("popper");
var d = {
data() {
...
},
created: function() {
// Load data with $http module
},
updated: function() {
$(function () {
$('[data-toggle="popover"]').popover()
})
},
};
export default d;
</script>
該按鈕僅在加載后才會出現,因為它有一個父元素,該元素對使用 ajax 加載的數據進行了v-for
綁定。
我不知道如何要求popper
,所以行$('[data-toggle="popover"]').popover()
解決了(它找不到函數popover()
)
此外,行const popper = require("poppper")
也不起作用,錯誤Module parse failed: 'return' outside of function
。 我的猜測是我不能用require
加載popper
,因為它不是為它構建的。
經過一番掙扎和嘗試完全隨機的想法之后,我有了一個有效的想法。 原來問題是我使用的是作為包安裝到ASP.NET MVC
中的bootstrap
程序(因此它將它作為<script src='..'/>
到頁面中)。 所以在我之后:
npm install --save bootstrap
新增bootstrap: 'bootstrap'
到ProvidePlugin
定義webpack.config.js
require("bootstrap")
到我的 vue 文件中它開始工作了。 由於某種原因,我什至require 'popper'
——可能是因為bootstrap
已經包含它?
雖然我仍然不確定這是否是解決問題的正確方法
使用 Vuejs 指令
const bsPopover = (el, binding) => {
const p = []
if (binding.modifiers.focus) p.push('focus')
if (binding.modifiers.hover) p.push('hover')
if (binding.modifiers.click) p.push('click')
if (!p.length) p.push('hover')
$(el).popover({
animation: false,
placement: binding.arg || 'top',
trigger: p.join(' '),
html: !!binding.modifiers.html,
content: binding.value,
});}
Vue.directive('tooltip', {
bind: bsTooltip,
update: bsTooltip,
unbind (el) { $(el).tooltip('dispose') }});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.