我使用VS 2015作为我的IDE。我看过以下内容: Angular 2 typescript invoke javascript function
和
Using a Javascript Function from Typescript
和
Using a Javascript Function from Typescript
然而,我仍然没有得到基本的。假设我有testing.js 和app.component.ts
var testJs = function () {
this.asd = 123;
}
testJs.prototype.testing = function (param) {
console.log(param);
}
如何在app.component.ts中使用testJ?目前我尝试了但失败了:
import { Component } from '@angular/core';
interface testJs {
testing: Function;
}
declare var testJs: testJs;
@Component({
selector: 'my-app',
templateUrl: '/mainTemplate.html'
})
export class AppComponent {
testJs.testing("adsf");
}
我没有angular.cli。没有angular.cli可以做到吗?
答案 0 :(得分:4)
终于找到了办法。
(在这种情况下我使用的是角度)执行以下操作:
一个。声明变量:
湾在导出的类
的构造函数中使用funcitonimport { Component } from '@angular/core';
declare function testJs(): any;
@Component({
selector: 'my-app',
templateUrl: '/mainTemplate.html'
})
export class AppComponent {
user: string;
constructor() {
testJs.prototype.testFunction();
this.user = "asdf";
var x = 90;
}
}
答案 1 :(得分:3)
在哪里获取此PoJo
。这个外部库吗?如果是的话,你需要将它包含在angular-cli中:
testJS.js
您需要创建打字或至少创建虚拟类型:
"scripts": [ "../node_modules/.../testJS.js" ],
您可以在
答案 2 :(得分:0)
我在eval()中使用
1)在main.js文件之前,放入javascript文件。
2)从您的打字稿文件的eval(“ testFunction()”)中使用angular。