为什么我的 bootstrap 5 弹出框返回 Uncaught TypeError?

问题描述

我想在我的 Angular 应用中使用 Bootstrap 5 弹出框。

这就是我所做的:

我的 index.html

<!doctype html>
<html lang="en">
<head>
  <Meta charset="utf-8">
  <title>my website</title>
  <base href="/">
  <Meta name="viewport" content="width=device-width,initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJfdroafW" crossorigin="anonymous"></script>
  <script>
    var popover = new bootstrap.Popover(document.querySelector('.popoverable'),{
      container: 'body'
    })
  </script>
</body>
</html>

app.component.html

<button type="button" class="btn btn-secondary popoverable" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Bottom popover">
  Popover on bottom
</button>

然后在我的页面上我看到了我的样式按钮,但没有出现弹出框,并且在控制台中我得到一个 Uncaught TypeError: this._element is undefined

有人能解决我的问题吗?

解决方法

您使用的是 bootstrap 5 的测试版,它不需要 jQuery 并使用自己的 javascript 文件。

要初始化 bootstrap 的 js,您必须使用 bs

替换:

  • data-toggledata-bs-toggle
  • data-placementdata-bs-placement
  • data-contentdata-bs-content

您的代码:

<button type="button" class="btn btn-secondary popoverable" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Bottom popover">
  Popover on bottom
</button>

更新代码:

<button type="button" class="my-5 d-block btn btn-secondary popoverable" data-container="body" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content="Bottom popover">
  Popover on bottom
</button>

enter image description here