Javascript Typescript-不同文件中的模块函数引用-“;找不到符号“;
如果答案很简单,或者我误读了typescript文档,我深表歉意,但是 我有一个模块:Javascript Typescript-不同文件中的模块函数引用-“;找不到符号“;,javascript,typescript,web-essentials,Javascript,Typescript,Web Essentials,如果答案很简单,或者我误读了typescript文档,我深表歉意,但是 我有一个模块: module Utils{ export function MyFunction(str: string):string { return // something to do with string } } 我想在另一个.ts文件(Clients.ts)中使用它,因此在顶部添加一个引用并尝试使用它: /// <reference path="Utils.ts" /
module Utils{
export function MyFunction(str: string):string {
return // something to do with string
}
}
我想在另一个.ts文件(Clients.ts)中使用它,因此在顶部添加一个引用并尝试使用它:
/// <reference path="Utils.ts" />
Utils.MyFunction(str);
谁能解释一下我做错了什么
使用VS2012、Web Essentials和TypeScript 0.9.1
谢谢。您遇到的错误
错误TS5037:除非提供“-module”标志,否则无法编译外部模块。
不是您的代码会遇到的问题
只有当您在文件的根级别导出某些内容时,您才会得到这个结果。e、 g
export module Utils{ // Notice the export keyword
export function MyFunction(str: string):string {
return // something to do with string
}
}
在这种情况下,您使用的是外部模块加载器,typescript需要知道它的amd
(requirejs/browser)还是commonjs
(节点/服务器端)
PS:我做了一个关于这个主题的视频:我自己找到了答案。我实际上在寻找一个类上的静态方法。根据以下规定:
class Utils {
static MyFunction(str: string): string {
return //... do something with string
}
}
这在Client.ts中起了作用
/// <reference path="Utils.ts" />
var x = Utils.MyFunction(str);
//
var x=Utils.MyFunction(str);
这不是我的问题。将在下面发布答案。
/// <reference path="Utils.ts" />
var x = Utils.MyFunction(str);