3 回答

TA貢獻1815條經驗 獲得超10個贊
理想情況下,您需要具有.d.ts鍵入文件才能Linting正常工作。
但是似乎d3gauge沒有,您可以要求開發人員提供并希望他們會聽。
或者,您可以通過執行以下操作解決此特定問題
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)文件中引用它,就像這樣
///<reference path="../path/to/d3gauage.d.ts"/>

TA貢獻1735條經驗 獲得超5個贊
在浪費大量時間尋找解決方案之后,我找到了一個。為了方便起見,我使用了完整的代碼,可以用它替換整個文件。
這是一個普遍的答案。假設您要將名為testjs.js的文件導入您的angular 2組件。在您的資產文件夾中創建testjs.js:
資產> testjs.js
function test(){
alert('TestingFunction')
}
在您的index.html中包含testjs.js
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Project1</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="./assets/testjs.js"></script>
</head>
<body>
<app-root>Loading...</app-root>
</body>
</html>
在您要調用此js的app.component.ts或任何component.ts文件中,聲明一個變量并調用如下函數:
app.component.ts
import { Component } from '@angular/core';
declare var test: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
f(){
new test();
}
}
最后在您的app.component.html中測試功能
app.component.html
<h1>
<button (click)='f()'>Test</button>
</h1>

TA貢獻1852條經驗 獲得超1個贊
您可以在文件中包含js擴展名,而不必在中添加文件擴展名。index.html.angular-cli-json
這些是我執行此步驟的步驟:
首先將您的外部js文件包含在assets/js
在.angular-cli.json-在腳本下添加文件路徑: [../app/assets/js/test.js]
在要使用js文件功能的組件中。
在頂部將文件導入為聲明
declare const Test:any;
之后,您可以訪問其功能,例如 Test.add()
添加回答
舉報