从打字稿角2调用javascript函数 [英] calling javascript function from typescript angular 2

查看:73
本文介绍了从打字稿角2调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2015作为我的IDE.我已阅读以下内容:

,我没有angular.cli.不用angular.cli就能做到吗?

解决方案

最后找到了一种方法.

    在下载main.js文件的html文件中的
  1. ,将对javascript文件的引用放在head标签中

  2. 在您的打字稿文件中(在这种情况下,我使用的是
  3. ),请执行以下操作:

a.声明变量:

b.在导出的类的构造函数中使用功能

 从'@ angular/core'导入{组件};声明函数testJs():任意;@零件({选择器:"my-app",templateUrl:"/mainTemplate.html"})导出类AppComponent {用户:字符串;Constructor(){testJs.prototype.testFunction();this.user ="asdf";var x = 90;}} 

I am using VS 2015 as my IDE. I have read the following: Angular 2 typescript invoke javascript function

and

Using a Javascript Function from Typescript

and

Using a Javascript Function from Typescript

however, i still don't get the basic.

assuming that i have testing.js and 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:

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?

解决方案

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

  2. 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;
    }
}

这篇关于从打字稿角2调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆