可以在不使用exports命令的情况下编写此类型脚本
Possible to write this typescript without using the exports command
学习TypeScript并拥有一个简单的Angular服务:
interface IBaConfigFactory {
dateTimeNow: Date;
}
export class BaConfigFactory implements IBaConfigFactory {
dateTimeNow: Date;
constructor() {
this.dateTimeNow = new Date();
}
}
angular
.module("blogApp")
.service("BaConfigFactory", BaConfigFactory);
但是brower抱怨道:
baConfigService.ts:6未捕获引用错误:未定义导出(匿名函数)@baConfigService:ts:6 app.ts:48
根据我所学到的,我需要像Webpack或CommonJS这样的东西来让浏览器了解导出。有没有可能重写它,这样我就不会收到导出命令?专注于一次学习一件事。
如果不使用模块系统加载文件,并且不将代码放在模块/名称空间中,则不需要导出。
例如,这应该可以正常工作:
class MyClass {
private x: number;
constructor(x: number) {
this.x = x;
}
getX(): number {
return this.x;
}
doit(y: number): number {
return this.x * y;
}
}
function echo(value: any): any {
return value;
}
(sample.ts)
<html>
<head>
<script src="example.js"></script>
<script>
var a1 = new MyClass(10),
a2 = new MyClass(43);
console.log(echo("hey there!"));
console.log(a1.doit(a2.getX()));
</script>
</head>
<body></body>
</html>
但是,使用export
应该很好,因为它不应该出现在编译后的js中(只是确保不要在编译器选项中使用-m
或--module
)。
例如:
module MyModule {
export class MyClass {
private x: number;
constructor(x: number) {
this.x = x;
}
getX(): number {
return this.x;
}
doit(y: number): number {
return this.x * y;
}
}
export function echo(value: any): any {
return value;
}
}
被编译为:
var MyModule;
(function (MyModule) {
var MyClass = (function () {
function MyClass(x) {
this.x = x;
}
MyClass.prototype.getX = function () {
return this.x;
};
MyClass.prototype.doit = function (y) {
return this.x * y;
};
return MyClass;
}());
MyModule.MyClass = MyClass;
function echo(value) {
return value;
}
MyModule.echo = echo;
})(MyModule || (MyModule = {}));
(操场上的代码)
您会注意到,在编译后的js中没有export
然后你简单地:
<html>
<head>
<script src="example.js"></script>
<script>
var a1 = new MyModule.MyClass(10),
a2 = new MyModule.MyClass(43);
console.log(MyModule.echo("hey there!"));
console.log(a1.doit(a2.getX()));
</script>
</head>
<body></body>
</html>
相关文章:
- 使用php脚本验证访问者的年龄,并在不刷新的情况下根据结果加载iframe
- 我如何让jQuery使用Greasemonkey 0.8脚本(在Firefox 2上),在没有互联网的情况下使用PC
- 在php中不运行java脚本的情况下重定向页面
- 如何在我的情况下创建加载脚本
- 使用javascript:在没有阻止html标记(<b>、<p>等)的情况下,阻止脚本的最佳方法
- 在谷歌应用程序脚本中,如何在满足条件的情况下只发送一次电子邮件
- 是否可以在不使用服务器端脚本或编程的情况下在网页中进行实时视频流
- 可以在不使用exports命令的情况下编写此类型脚本
- Jquery脚本在没有警报的情况下无法工作
- 如何在没有网络工作者的情况下运行多个脚本
- 可以插入<脚本>标记可以在不使用JavaScript中的eval的情况下多次运行
- 在这种特殊情况下,在PHP脚本之间传递值
- 如何在不阻塞脚本的情况下运行脚本
- Node.JS在没有停止Node.JS脚本的情况下再次运行NodeMsql查询
- 谷歌应用程序脚本:如何在不使用getComment()方法的情况下将第二条注释添加到第一条注释中
- Javascript在不更改脚本标记的情况下阻止缓存
- 如何在不添加html脚本标记的情况下使用google-maps-api-v3
- 谷歌应用程序脚本:如何在没有用户交互的情况下调用绑定到处理程序的函数
- 如何在不使用脚本标记的情况下注入javascript
- 是否可以在不重新加载的情况下运行注入的脚本