繁体   English   中英

Bootstrap 4 下拉菜单不适用于 Angular 12

[英]Bootstrap 4 dropdown not working with Angular 12

我有一个带有 Bootstrap 4 的示例 Angular 12 项目。除下拉列表外,我的项目中的所有内容都在工作。

这个 SO 答案对我不起作用。

我已经使用给出的 NPM 命令在我的项目中安装了 jQuery、Popper 和 Bootstrap:

npm install --save @popperjs/core
npm install --save bootstrap
npm install --save jquery

我还从 angular.json 中提到了它们:

"styles": [
     "node_modules/bootstrap/dist/css/bootstrap.min.css",
     "src/styles.css"
],
"scripts": [
     "node_modules/jquery/dist/jquery.min.js",
     "node_modules/@popperjs/core/dist/umd/popper.min.js",
     "node_modules/bootstrap/dist/js/bootstrap.min.js"
]

我没有收到任何错误,而且下拉按钮也来了。 但问题是,当我单击时,列表并没有弹出。

下面给出了我从Bootstrap Dropdown Official Docs获取的示例代码进行测试。

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

我错过了一些需要添加的东西吗?

它对我有用

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

编辑:天哪。 这个问题对我来说很累。 我工作了几个小时。 并且问题以一种被忽视的方式解决了,我不明白原因。 我不得不为这个大声笑阅读 bootstrap.js。 只需将 html 文件中的表达式data-toggle="dropdown"更改为data-bs-toggle="dropdown"即可。

所以:

在 html

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

注意:我上面分享的angular.json示例仍在运行。

是的,解决方案对我有用,谢谢!

但是我没有按照上面的方法更新 angular.json,而是在 app.module.ts 中添加了引导下拉模块,即:

import { BsDropdownModule } from 'ngx-bootstrap/dropdown';

@NgModule({
  declarations: [
    .....
  ],
  imports: [
    ...
    ,BsDropdownModule.forRoot()
    ...
  ]
  ,bootstrap: [AppComponent]
})
export class AppModule { }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM