且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular2:将外部js文件导入组件

更新时间:2023-11-22 17:45:04

理想情况下,您需要 .d.ts 文件才能让 Linting 工作。

Ideally you need to have .d.ts file for typings to let Linting work.

但似乎 d3gauge 没有一,你可以要求开发商提供并希望他们会倾听。

But It seems that d3gauge doesn't have one, you can Ask the developers to provide and hope they will listen.

或者,您可以通过以下方式解决此问题:这样做

Alternatively, you can solve this specific issue by doing this

declare var drawGauge: any;

import '../../../../js/d3gauge.js';
export class MemMonComponent {
    createMemGauge() {
        new drawGauge(this.opt);  //drawGauge() is a function inside d3gauge.js
    }
}






如果您在多个文件中使用它,您可以使用以下内容创建 d3gauage.d.ts 文件

declare var drawGauge: any;

并在 boot.ts 中引用它(bootstrap)文件位于顶部,如下所示

and reference it in your boot.ts (bootstrap) file at the top, like this

///<reference path="../path/to/d3gauage.d.ts"/>