简体   繁体   English

在Angular2组件中使用Bootstrap导航

[英]Using Bootstrap navs into Angular2 component

I'm trying to use Bootstrap's navs into my Angular 2 app. 我正在尝试在我的Angular 2应用程序中使用Bootstrap的导航。

I followed the example (copy / paste) in the Bootstrap docs ( https://v4-alpha.getbootstrap.com/components/navs/#javascript-behavior ). 我遵循了Bootstrap文档( https://v4-alpha.getbootstrap.com/components/navs/#javascript-behavior )中的示例(复制/粘贴)。 I've included all the required CSS and JS in the .angular-cli.json file 我已经在.angular-cli.json文件中包含了所有必需的CSS和JS

 "styles": [
    "styles.css",
    ....
    "../node_modules/bootstrap/dist/css/bootstrap.css",
    "../node_modules/bootstrap/dist/css/bootstrap-grid.css",
    "../node_modules/bootstrap/dist/css/bootstrap-reboot.css",
    ....
  ],
  "scripts": [
    ....
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/bootstrap/dist/js/bootstrap.js"
  ]

but, even if it is rendered correctly, events on click are not handled (nothing happens on view). 但是,即使呈现正确,点击事件也不会被处理(视图上什么也没有发生)。

I wouldn't use ng-boostrap, only if necessary. 仅在必要时,我不会使用ng-boostrap。 If someone could also explain how external 3rd party javascript libs (no typescript, just vanilla JS) could be used inside A2+ apps, I would appreciate. 如果有人还可以解释如何在A2 +应用程序中使用外部第三方javascript库(没有打字稿,只有普通JS),我将不胜感激。

EDIT 编辑

I think be helpful to know that the component in which the navbar is nested is dynamically created in a different moment from app loading (so when the scripts are executed). 我想知道嵌套导航栏的组件是在与应用程序加载不同的时刻动态创建的(因此在执行脚本时)会有所帮助。 Could it be the reason why no click handler is bound to elements? 可能是没有单击处理程序绑定到元素的原因吗? If it so, how can I fix this? 如果是这样,我该如何解决?

EDIT 2 编辑2

I've tried to use Boostrap in a component that has not been loaded at runtime (in the app-root component)... And works. 我尝试在运行时尚未加载的组件中使用Boostrap(在app-root组件中)...并且可以工作。 So, why in a dynamically created component at runtime, this is not working anymore? 那么,为什么在运行时动态创建的组件中不再起作用?

The bootstrap 4 beta and alpha are currently having problems with popper.js. bootstrap 4 beta和alpha当前与popper.js有关。 Personally I had to use Bootstrap 3 instead for my angular 4 project. 就我个人而言,我必须在我的angular 4项目中使用Bootstrap 3。

Check your console for misfiring javascript. 检查您的控制台是否未启用JavaScript。

EDIT 编辑

These are the errors I get in my console when trying to run it. 这些是尝试运行控制台时在控制台中出现的错误。 popper error 弹出错误

EDIT2 编辑2

I don't think load time is relevant as long as the javascript is loaded by the time it should be used on the html. 我认为加载时间无关紧要,只要在html上使用javascript时就已加载javascript。 If this is not the case maybe lifecycle hooks can help you. 如果不是这种情况,那么生命周期挂钩可能会为您提供帮助。 https://angular.io/guide/lifecycle-hooks https://angular.io/guide/lifecycle-hooks

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

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