简体   繁体   English

Angular 2 typescript调用javascript函数

[英]Angular 2 typescript invoke javascript function

Is there a correct way to invoke a JavaScript function from a component in Angular 2 (TypeScript) ? 是否有正确的方法从Angular 2(TypeScript)中的组件调用JavaScript函数?

Here is my component : 这是我的组件:

import { ElementRef, AfterViewInit }       from '@angular/core';

export class AppComponent implements AfterViewInit {

    constructor(private _elementRef: ElementRef) {
    }

    ngAfterViewInit() {
        /**
         * Works but i have this error :
         * src/app.component.ts(68,9): error TS2304: Cannot find name 'MYTHEME'.
         * src/app.component.ts(69,9): error TS2304: Cannot find name 'MYTHEME'.
         */
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();

        /**
         * Works without error, but doesn't seem like a right way to do it
         */
        var s = document.createElement("script");
        s.text = "MYTHEME.documentOnLoad.init(); MYTHEME.documentOnReady.init();";
        this._elementRef.nativeElement.appendChild(s);
    }
}

Calling the JavaScript function directly result in an compilation error, but the syntax in the "compiled" JavaScript file (app.component.js) is correct : 直接调用JavaScript函数会导致编译错误,但“已编译”的JavaScript文件(app.component.js)中的语法是正确的:

AppComponent.prototype.ngAfterViewInit = function () {
    MYTHEME.documentOnLoad.init();
    MYTHEME.documentOnReady.init();
};

The 2nd way (appendChild) works without error, but i don't think (altering the DOM from typescript/angular) is the correct way to do it. 第二种方式(appendChild)工作没有错误,但我不认为(从typescript / angular更改DOM)是正确的方法。

I found this : Using a Javascript Function from Typescript I tried declaring the interface : 我发现了这个: 使用Typescript中的Javascript函数我尝试声明了界面:

interface MYTHEME {
    documentOnLoad: Function;
    documentOnReady: Function;
}

But the TypeScript doesn't seem to recognize it (no error in the interface declaration). 但是TypeScript似乎没有识别它(接口声明中没有错误)。

Thanks 谢谢

Edit : 编辑:

Following the answer by Juan Mendes this is what i ended with : 在Juan Mendes的回答之后,这就是我的结局:

import { AfterViewInit }       from '@angular/core';

interface MYTHEME {
    documentOnLoad: INIT;
    documentOnReady: INIT;
}
interface INIT {
    init: Function;
}
declare var MYTHEME: MYTHEME;

export class AppComponent implements AfterViewInit {

    constructor() {
    }

    ngAfterViewInit() {
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();
    }
}

You have to tell TypeScript about external (JavaScript) declarations using declare . 您必须使用declare告诉TypeScript有关外部(JavaScript)声明的信息。 See https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html 请参阅https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html

interface MyTheme {
    documentOnLoad: Function;
    documentOnReady: Function;
}
declare var MYTHEME: MyTheme;

Or anonymously 或者匿名

declare var MYTHEME: {documentOnLoad: Function, documentOnReady: Function};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM