【问题标题】:How to init Bootstrap Popover in Webpack Rails project如何在 Webpack Rails 项目中初始化 Bootstrap Popover
【发布时间】:2021-11-09 06:12:12
【问题描述】:

所以我关注了bootstrap doc on who to init the popover,但它不起作用。它抛出了这个错误。我该如何解决这个问题?

Uncaught TypeError: bootstrap.Popover is not a constructor

这些是代码。

test_popover.html

  <button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" title="Popover title" 
          data-bs-content="And here's some amazing content. It's very engaging. Right?">
    Click
  </button>

test_popover.js

  ...
  var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
  var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
    return new bootstrap.Popover(popoverTriggerEl)
  })

我已在 packs/application.js 中导入引导程序

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
    

// Import Bootstrap only ones that are used to reduce size
import 'bootstrap';
import '../javascripts/test_popover';

【问题讨论】:

    标签: ruby-on-rails webpack bootstrap-5 ruby-on-rails-6.1


    【解决方案1】:

    这就是我修复它的方法。我导入Popover 并从新的bootstrap.Popover() 更改为new Popover()

    import Popover from 'bootstrap/js/dist/popover';
    
    document.addEventListener("turbolinks:load", function() {    
      var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
      var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
        return new Popover(popoverTriggerEl)
      })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      相关资源
      最近更新 更多