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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:28:46  来源:igfitidea点击:

calling javascript function from typescript angular 2

visual-studioangulartypescript

提问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. enter image description here

我已经尝试了以下方法,但仍然无法正常工作。 在此处输入图片说明

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.

终于找到了实现的方法。

  1. in your html file that download the main.js file, put reference to the javascript file in the head tag enter image description here

  2. in your typescript file (in this case i am using angular) do the following:

  1. 在下载 main.js 文件的 html 文件中,在 head 标记中引用 javascript 文件 在此处输入图片说明

  2. 在您的打字稿文件中(在本例中我使用的是 angular),请执行以下操作:

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

  1. 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" ],

  2. You need to either create typings or at least make dummy type:

    declare var testJS: any;

  3. You can use it afer

  1. 在哪里得到这个testJS.js。这是外部库吗?如果是,您需要像这样将它包含在 angular-cli 中:

    "scripts": [ "../node_modules/.../testJS.js" ],

  2. 您需要创建类型或至少创建虚拟类型:

    declare var testJS: any;

  3. 您可以稍后使用它

回答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()") 。