本文介绍了在Typescript中,如何使用另一个TS文件中定义的函数而不将它们放在模块中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用TS文件中定义的函数,这个文件将称为library.ts。我需要在另一个文件main.ts中使用这些函数。但是,出于良好的非技术原因(教育),我不希望用户必须了解模块。



例如,我只希望他们能够调用ReadText / WriteText,而不必担心模块。



如何从main.ts函数中调用不在library.ts模块中定义的函数?



我的VS项目说我正在使用Typescript 1.1(TypeScriptToolsVersion)

解决方案



这里是Library.ts:

  var globalVariable ='Hello World'; 

函数globalFunction(){
alert(globalVariable);
}

这里是app.ts:

  ///< reference path =library.ts/> 

globalFunction();


I need to use functions defined in a TS file, a file that I'll call "library.ts". I need to use these functions in another file "main.ts". However, for good non-technical reasons (education), I do not want the user to have to know about modules.

For example, I just want them to be able to call ReadText/WriteText without having to worry about a module. X.ReadText is unacceptable.

How can I call a function defined not within a module in library.ts from a function in main.ts?

My VS project says I'm using Typescript 1.1 (TypeScriptToolsVersion)

解决方案

You can put any code in a file, it doesn't have to be a module.

Here is Library.ts:

var globalVariable = 'Hello World';

function globalFunction() {
    alert(globalVariable);
}

And here is app.ts:

/// <reference path="library.ts" />

globalFunction();

这篇关于在Typescript中,如何使用另一个TS文件中定义的函数而不将它们放在模块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:41