【问题标题】:Angular issues with Bootstrap componentsBootstrap 组件的角度问题
【发布时间】:2021-08-06 03:50:39
【问题描述】:

我正在使用 Angular 12 构建网站,我认为这不相关,但我正在使用 sccs。 我在使用引导模块时遇到问题,例如下拉、模式和工具提示,它们基本上是我最需要的组件。 我使用“npm install bootstrap”安装了引导模块。 我的脚本数组是这样配置的:

"scripts": [ 
"node_modules/jquery/dist/jquery.slim.min.js", 
"node_modules/bootstrap/dist/js/bootstrap.bundle.min.js" ]

我尝试使用的下拉列表代码是: https://getbootstrap.com/docs/5.1/components/dropdowns/

<div class="btn-group">
  <button type="button" class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Action
  </button>
  <ul class="dropdown-menu">
    <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>
    <li><hr class="dropdown-divider"></li>
    <li><a class="dropdown-item" href="#">Separated link</a></li>
  </ul>
</div>

你能帮帮我吗?

【问题讨论】:

  • 按钮是否像在 Bootstrap 中一样?
  • 是的,它看起来像在引导程序中,但点击时没有任何反应

标签: html angular bootstrap-5


【解决方案1】:

您只需导入 Popperjs 两次导致问题,因此删除 popperjs 和 bootstrap.min.js 导入将解决您的问题,否则请按照以下给定步骤操作

在 Angular 中使用引导程序的步骤。

  1. 安装引导程序

    • npm install bootstrap --save
  2. angular.json 文件中

    • 样式数组下导入bootstrap.min.css

      • "node_modules/bootstrap/dist/css/bootstrap.min.css"
    • scripts array 下导入 bootstrap.bundle.min.js 文件,因为 popper.js 包含在 bootstrap.bundle.js 文件中。如果你只导入 bootstrap.js 那么你必须在 bootstrap.js 之前导入 popper.js 以获取更多信息Check Bootstrap Docs

      • "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"

Angular.json file content

对于 Jquery

  1. 如果您还想使用 jquery,请在 bootstrap.bundle.min.js 之前导入 jquery。

  2. 从 npm 安装 jquery 类型

    • npm install --save @types/jquery
  3. angular.json文件中导入jquery

    • "node_modules/jquery/dist/jquery.min.js"
  4. tsconfig.app.json文件的compilerOptions

    下添加jquery类型
    • "types": ["jquery"]
  5. 然后使用jquery著名的$变量

  6. 享受

jsconfig.app.json file content

更新

如果您使用的是 scss,则将引导程序 scss 文件导入样式数组而不是 css 文件。

在样式数组下的 angular.json 文件中导入引导 scss 文件 node_modules/bootstrap/scss/bootstrap.scss

【讨论】:

  • 这取决于他们使用的版本 bootstrap 5 使用 vanilla javascript。操作员应该将他的按钮发布在 html 和打字稿上,这样我们就可以看到发生了什么。如果按钮出现 - 您是否分配了(单击)功能?你用它作为路由器链接吗?
  • @chrisburd 实际上问题出在导入文件中。他两次导入 popperjs。一个来自@popper/core,另一个来自 bootstrap.bundle.min.js。删除 bootstrap.bundle.min.js 或 [popperjs 和 bootstrap.min.js] 都可以解决问题
  • @FaizShahid 我在写这篇文章之前做了你写的一切,只有样式有效,我认为 js 出于某种原因没有加载
  • @GiuseppeParisi 这是因为您两次导入 popperjs。只需保留 jquery,然后将 bootstrap.bundle.min.js 保留在脚本数组中,否则将其删除
  • @FaizShahid 我像你说的那样改变了我的 angular.json 但问题仍然存在,现在我的脚本数组就像这样 "scripts": [ "node_modules/jquery/dist/jquery.slim.min. js", "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js" ]
【解决方案2】:

Angular 组件是动态创建的,因此在页面加载时对元素执行操作的任何脚本在 Angular 模板中使用时都不会自动执行任何操作。

稍微搜索一下,你会发现在 Angular 上使用 Bootstrap 的库,例如:https://ng-bootstrap.github.io

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-01-01
  • 2020-07-16
  • 2021-08-13
  • 1970-01-01
  • 2022-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多