[英]Angular 9 - js scripts not working (e.g. foundations or bootstrap)
First steps in Angular (doing an online course) and I keep thinking that my JS is not working. Angular(做在线课程)的第一步,我一直认为我的 JS 不工作。 Because when I ng serve the below project is see the foundation styling (css), but the hamburger Icon is not opening/closing when I click it.
因为当我为下面的项目服务时,会看到基础样式(css),但是当我单击它时,汉堡包图标没有打开/关闭。 The code is a sample from the foundation website.
该代码是来自基金会网站的示例。
app.component.html looks like this app.component.html 看起来像这样
<h1>This is the app</h1>
<div class="title-bar" data-responsive-toggle="main-nav" data-hide-for="medium">
<button class="menu-icon" type="button" data-toggle="main-nav"></button>
<div class="title-bar-title">Menu</div>
</div>
<div class="top-bar" id="main-nav">
<div class="top-bar-left">
<ul class="dropdown vertical medium-horizontal menu" data-dropdown-menu>
<li class="menu-text">Site title</li>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Two</a></li>
</ul>
</div>
</div>
Through numerous topics in ended up with the following:通过众多主题最终得出以下结论:
In styles.css I added在 styles.css 我添加
@import "../node_modules/foundation-sites/dist/css/foundation.min.css";
In angular.json I added在 angular.json 我添加
"styles": [
"src/styles.css",
"node_modules/foundation-sites/dist/css/foundation.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/foundation-sites/dist/js/foundation.min.js"
]
My own thoughts on it:我个人对此的看法:
It's probably possible to get this to work, but plain-old Bootstrap uses jQuery to manipulate the DOM (eg open/close your toolbar when you click the hamburger menu icon).这可能是可行的,但普通的 Bootstrap 使用 jQuery 来操作 DOM(例如,当您单击汉堡菜单图标时打开/关闭您的工具栏)。
jQuery really should not be used with Angular, because it could interfere with Angular's maintenance of the DOM. jQuery 真的不应该与 Angular 一起使用,因为它可能会干扰 Angular 对 DOM 的维护。 (jQuery would change the DOM, and Angular wouldn't know about it).
(jQuery 会改变 DOM,而 Angular 不会知道)。
For this reason, there are a couple of other projects, including ngx-bootstrap, that either use Bootstrap's css but implement the js functionality in terms of Angular directives, or which look and operate like Bootstrap and re-implement (at least partially) the css and the js.出于这个原因,还有一些其他项目,包括 ngx-bootstrap,它们要么使用 Bootstrap 的 css,但根据 Angular 指令实现 js 功能,或者看起来和操作类似于 Bootstrap 并重新实现(至少部分地) css 和 js。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.