简体   繁体   English

如何在JavaScript文件中调用Typescript方法

[英]How to call Typescript method in JavaScript file

We have a typescript class,which contains few methods. 我们有一个打字稿类,其中包含一些方法。 Sample example of the TypeScript class. TypeScript类的示例示例。

 export class AppointmentListComponent implements OnInit {
    editEvent(event: IEvent): void {
        this._router.navigate(['/appointments', event.id]);
    }
 }

We want to call editEvent method from another (.js) file. 我们想从另一个(.js)文件中调用editEvent方法。 How can we achieve this. 我们如何实现这一目标。

If you are working with ES6 it's pretty simple. 如果您使用的是ES6,则非常简单。 In typescript just use import "pathToJSModule"; 在打字稿中,只需使用import "pathToJSModule"; and you are ready to use it. 您就可以使用它了。 This is possible because JS is a valid Typescript. 这是可能的,因为JS是有效的Typescript。

If you are not working with ES6 you can use import * as foo from "pathToFile" . 如果您不使用ES6,则可以import * as foo from "pathToFile"使用import * as foo from "pathToFile"

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

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