typescript 从打字稿角度2调用javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43530096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling javascript function from typescript angular 2
提问by jay
I am using VS 2015 as my IDE. I have read the following: Angular 2 typescript invoke javascript function
我使用 VS 2015 作为我的 IDE。我已阅读以下内容: Angular 2 typescript invoke javascript function
and
和
Using a Javascript Function from Typescript
使用 Typescript 中的 Javascript 函数
and
和
Using a Javascript Function from Typescript
使用 Typescript 中的 Javascript 函数
however, i still don't get the basic.
然而,我仍然没有得到基本的。
assuming that i have testing.js and app.component.ts
假设我有 testing.js 和 app.component.ts
var testJs = function () {
this.asd = 123;
}
testJs.prototype.testing = function (param) {
console.log(param);
}
How do I use testJs in my app.component.ts? at the moment i try this but failed:
如何在 app.component.ts 中使用 testJs?目前我尝试这样做但失败了:
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");
}
I have tried the following and it still does not work as well.
and I don't have angular.cli. is it possible to do it without angular.cli?
而且我没有 angular.cli。没有angular.cli可以做到吗?
回答by jay
Finally found a way to do it.
终于找到了实现的方法。
in your html file that download the main.js file, put reference to the javascript file in the head tag
in your typescript file (in this case i am using angular) do the following:
a. declare the variable :
一个。声明变量:
b. use the funciton in the constructor of the exported class
湾 在导出类的构造函数中使用函数
import { 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;
}
}
回答by Julia Passynkova
where is get this
testJS.js
. Is this external lib? if yes, you need to include it in angular-cli like that:"scripts": [ "../node_modules/.../testJS.js" ],
You need to either create typings or at least make dummy type:
declare var testJS: any;
You can use it afer
在哪里得到这个
testJS.js
。这是外部库吗?如果是,您需要像这样将它包含在 angular-cli 中:"scripts": [ "../node_modules/.../testJS.js" ],
您需要创建类型或至少创建虚拟类型:
declare var testJS: any;
您可以稍后使用它
回答by Mehran Kh
i'm use from eval()
1) before main.js file, put javascript file.
2) use from eval("testFunction()") in your typescript file in angular.
我在 eval()
1) 之前使用 main.js 文件,放入 javascript 文件。
2)在你的打字稿文件中以角度使用 from eval("testFunction()") 。