【发布时间】:2019-06-21 18:07:55
【问题描述】:
我在 Angular 7 应用程序中遇到了这个问题。我无法使用纯 Bootstrap CSS/JS 和 Popper.js 重现它。
我正在尝试使用 vanilla Bootstrap JS 和 jQuery,因为我需要 Bootstrap 的 toast,而其他库(ng-bootstrap 和 ngx-bootstrap)目前不支持 toast。
最小(非)工作示例
https://github.com/Crocmagnon/angular-bootstrap4-dropdown-issue
使用npm run start 运行它
指向 Popper.js 存储库中问题的链接
我试图问开发者,但他一无所知:https://github.com/FezVrasta/popper.js/issues/748
重现问题的步骤
用例子重现:
npm i -g @angular/cli
git clone https://github.com/Crocmagnon/angular-bootstrap4-dropdown-issue.git
cd angular-bootstrap4-dropdown-issue
npm i
ng serve
从头开始复制:
- 使用 ng-cli 创建 Angular 应用
- 安装引导程序和引导程序类型:
-
npm install --save bootstrap @types/bootstrap。 -
@types/bootstrap需要popper.js作为依赖项。
-
- 包括 Bootstrap 4 CSS 和 JS 文件
- 包含 Popper JS UMD 文件
- 在某处添加
import 'bootstrap';以告诉 typescript jQuery.tooltip()函数存在 - 尝试在导航栏中使用 Bootstrap 4 下拉菜单
来自angular.json的相关摘录:
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/@fortawesome/fontawesome-free/css/all.css",
"src/styles.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.slim.js",
"node_modules/popper.js/dist/umd/popper.js",
"node_modules/bootstrap/js/dist/util.js",
"node_modules/bootstrap/js/dist/alert.js",
"node_modules/bootstrap/js/dist/button.js",
"node_modules/bootstrap/js/dist/carousel.js",
"node_modules/bootstrap/js/dist/collapse.js",
"node_modules/bootstrap/js/dist/dropdown.js",
"node_modules/bootstrap/js/dist/modal.js",
"node_modules/bootstrap/js/dist/scrollspy.js",
"node_modules/bootstrap/js/dist/tab.js",
"node_modules/bootstrap/js/dist/toast.js",
"node_modules/bootstrap/js/dist/tooltip.js",
"node_modules/bootstrap/js/dist/popover.js"
]
预期的行为是什么?
下拉菜单应该切换
出了什么问题?
下拉菜单根本不切换
评论
我试图在我的angular.json 文件中包含 Popper ESM 文件。现在一切正常,除了控制台出现错误。
Uncaught SyntaxError: Unexpected token export
我错过了什么吗?
编辑
似乎main.ts 中的这条语句打破了下拉列表:
import 'bootstrap';
但是,如果我删除它,当我想做一些 Bootstrap jQuery (例如显示 toast 或工具提示)时,Typescript 会尖叫:
$(() => {
$('[data-toggle="tooltip"]').tooltip();
});
【问题讨论】:
-
您可以使用 Angular 实现的 Bootstrap 让您的生活更轻松,例如 ng-bootstrap。
-
你能解释一下为什么你试图在 Angular 中使用 Bootstrap JS + jQuery(和相关模块),而不是使用专门设计/开发的库中的组件/指令/模块,例如 ng-bootstrap使用 Angular 生命周期/渲染?你会在尝试使用原始 Bootstrap/jQuery 时遇到各种问题,因为渲染在 Angular 等框架中的工作方式完全不同。
-
ng-bootstrap和ngx-bootstrap没有 toasts,这是我在应用程序的另一部分需要的,这就是我使用 vanilla Bootstrap JS + jQuery 的原因。我将这个添加到问题中。 -
已编辑问题提及
import 'bootstrap';
标签: angular bootstrap-4 popper.js