可以在不使用 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);
但是浏览器抱怨:
baConfigService.ts:6 Uncaught ReferenceError: exports is not defined(anonymous function) @ baConfigService.ts:6 app.ts:48
根据我所学,我需要类似 Webpack 或 CommonJS 的东西来让浏览器理解导出。是否可以重写它,这样我就不会以导出命令结束?一次专注于学习一件事。
如果您不使用模块系统加载文件,并且不将代码放在 module/namepsace.
中,则不需要导出
例如,这应该可以正常工作:
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中(只是确保不要在[=19=中使用-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>
学习 TypeScript 并获得简单的 Angular 服务:
interface IBaConfigFactory {
dateTimeNow: Date;
}
export class BaConfigFactory implements IBaConfigFactory {
dateTimeNow: Date;
constructor() {
this.dateTimeNow = new Date();
}
}
angular
.module("blogApp")
.service("BaConfigFactory", BaConfigFactory);
但是浏览器抱怨:
baConfigService.ts:6 Uncaught ReferenceError: exports is not defined(anonymous function) @ baConfigService.ts:6 app.ts:48
根据我所学,我需要类似 Webpack 或 CommonJS 的东西来让浏览器理解导出。是否可以重写它,这样我就不会以导出命令结束?一次专注于学习一件事。
如果您不使用模块系统加载文件,并且不将代码放在 module/namepsace.
中,则不需要导出例如,这应该可以正常工作:
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中(只是确保不要在[=19=中使用-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>