在将Angular Cli从SystemJs切换到Webpack之后,我不确定如何包含JS文件(供应商).

For example

选项A

I have some js files that were installed via npm. Adding script tags to the head tag like this does not work. Nor does it seem like the best way.

<head>
   <script src="node_modules/some_package/somejs.js">
</head>

//With systemJs I could do this

<head>
   <script src="vendor/some_package/somejs.js">
</head>

选项B

Include these js files as part of the webpack bundle. This seems like the way it probably should be done. However I am not sure how to do this as all of the webpack code seems to be hidden behind the angular-cli-webpack node package. I was thinking maybe there is another webpack config that we might have access to. But I am not sure as I didn't see one when creating a new angular-cli-webpack project.

More Info:

我试图包含的js文件需要在Angular项目之前包含.例如jQuery和第三方js库,它们并不是真正为模块加载或typescript而设置的.

References https://github.com/angular/angular-cli/blob/master/WEBPACK_UPDATE.mdhttps://github.com/angular/angular-cli/tree/webpack

推荐答案

Last tested using angular-cli 11.x.x with Angular 11.x.x

这可以通过使用angular.json中的scripts:[]来实现.

{
  "project": {
    "version": "1.0.0",
    "name": "my-project"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": ["assets"],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false
  }
}

Note:在全局库安装中为the documentation suggests:如果更改styles(或scripts!)的值那么:

如果正在运行,请重新启动ng serve,

…通过scripts.bundle.js文件查看在**globalcontext中执行的脚本.

Note:如以下 comments 所述.通过es6 imports支持UMD模块的JS LIB,比如jQuery,也可以使用es6导入语法导入到typescript文件中.例如:import $ from 'jquery';.

Angular相关问答推荐

JS仅在客户端使用的时刻标准时间,无论时区如何

当我试图将日期放在x轴上时,Angular Highcharts区域平面图不工作

Angular - Bootstrap 5 Carbon不工作

未在ng bootstrap 模式中设置表单输入

如何重新显示ngbAlert(Bootstrap widgest for Angular)消息后再次驳回它

无法匹配任何路由.URL段:';英雄';

react 形式之间的循环关系

如何让 ag-Grid 事件读取私有方法?

从 applescript 调用Angular 前端以从列表中 Select 一个项目

ngrx 效果等到从初始值以外的其他状态收到响应

如何根据响应变化更改 Angular Carousel 的cellsToShow

TypeError:this.restoreDialogRef.afterClosed 不是函数

Angular 从 13.3.8 恢复到 13.3.7

个人修改 Angular CLI - 创建新应用

Angular模板错误:Property does not exist on type component

Observable .do() 运算符 (rxjs) 的用例

使用directive指令将类添加到host元素中

无法获得 angular-material md-sidenav 的 100% 高度

Angular:ViewChild 上未定义 nativeElement

在 RxJS Observable 中 flatten数组的最佳方法