简体   繁体   English

Angular 9 - js 脚本不工作(例如基础或引导程序)

[英]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:我个人对此的看法:

  • Hamburger Icon and styling is there but its not opening/closing汉堡图标和样式在那里,但它没有打开/关闭
  • My angular.json file is not named angular-cli.json (like in other examples...WHY??)我的 angular.json 文件未命名为 angular-cli.json (如在其他示例中......为什么??)
  • I did not needed to../ the scripts, they only compile like above (else error)我不需要../脚本,它们只能像上面那样编译(否则错误)
  • working on Unix (ubuntu)在 Unix (ubuntu) 上工作
  • Jquery version 3.5.1 and foundation-sites 6.6.3 Jquery 版本 3.5.1 和基础站点 6.6.3

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.

相关问题 使用相同的外部JS库(例如Angular)的Drupal 8多个自定义模块 - Drupal 8 Multiple Custom Modules Using Same External JS Lib (e.g. Angular) 捆绑的 JS 库,例如 angular、jQuery、lodash 在 eval 抛出“require is not defined”之后 - Bundled JS libraries e.g. angular, jQuery, lodash after eval throws "require is not defined" 如何在不请求外部页面的情况下重用angular-js通用标记(例如,小部件)? - How to reuse angular-js common mark up (e.g. widget) without requesting external page? 如何使用外部脚本,例如 jquery 用于 YouTrack Workflow 脚本 - How to use external scripts e.g. jquery for YouTrack Workflow scripts Vue JS扩展了标准输入控件,例如textarea - vue js extend standard input control e.g. textarea 没有 npm 的 video.js 插件(例如 eme) - video.js plugins (e.g., eme) without npm 如何在 Google Forms 上使用编程逻辑(例如 JS)? - How to use programming logic (e.g. JS) on Google Forms? Touch API(例如touchstart)无法在MS Edge中运行 - Touch API (e.g. touchstart) not working in MS Edge html5的验证器(例如 <input required> 不管用 - validators of html5 (e.g. <input required> is not working React 中的 Grape JS:默认块显示字符,例如 W、Q、E、R 而不是图标 - Grape JS in React : Default Blocks Showing Characters e.g. W, Q, E, R rather then icons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM