如何在angular 6库中添加外部js文件 [英] How to add external js file in angular 6 library

查看:32
本文介绍了如何在angular 6库中添加外部js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基于 angular 材料的 angular 库(版本 6),我需要为此包含hammer js 库.

我知道在angular 6中我们可以在项目配置下的angular.json中添加外部js库.但这在上述库的情况下不起作用.我尝试通过以下方式添加外部库.

我的图书馆":{"root": "项目/我的图书馆","sourceRoot": "projects/my-library/src","projectType": "图书馆",建筑师":{建造": {"builder": "@angular-devkit/build-ng-packagr:build",选项": {"tsConfig": "projects/my-library/tsconfig.lib.json","project": "projects/my-library/ng-package.json",脚本":[../node_modules/hammerjs/hammer.min.js"]}}}

但我收到此错误.

<块引用>

架构验证失败,出现以下错误:数据路径"不应具有附加属性(脚本).

请建议在 angular 库中添加外部 js 文件的正确方法是什么.

解决方案

关于 OP 尝试添加hammer.js 的具体示例.只需 npm installhammerjs 然后编辑您的 main.ts 文件并添加 import 'hammerjs' 然后它将在全球可用.

另请参阅这篇文章.>

I am creating an angular library (version 6) which is based on angular material for which I need to include the hammer js library.

I know that in angular 6 we can add the external js library in the angular.json under the project's configuration. But this does not work in case of above library. I tried to add the external library in the following way.

"my-library": {
  "root": "projects/my-library",
  "sourceRoot": "projects/my-library/src",
  "projectType": "library",
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-ng-packagr:build",
      "options": {
        "tsConfig": "projects/my-library/tsconfig.lib.json",
        "project": "projects/my-library/ng-package.json",
        "scripts": [
          "../node_modules/hammerjs/hammer.min.js"
        ]
      }
    }
}

But I am getting this error.

Schema validation failed with the following errors: Data path "" should NOT have additional properties(scripts).

Please suggest what is the correct way to add external js file in the angular library.

解决方案

Regarding the OP's specific example trying to add hammer.js. Just npm install hammerjs then edit your main.ts file and add import 'hammerjs' then it will be available globally.

Also see this article.

这篇关于如何在angular 6库中添加外部js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆