在export
和之间的Typescript有什么区别default export
.在所有教程中,我看到人们在export
他们的课程中,如果我default
在导出之前没有添加关键字,我就无法编译我的代码.
另外,我在官方打字稿文档中找不到任何默认导出关键字的痕迹.
export class MyClass {
collection = [1,2,3];
}
Run Code Online (Sandbox Code Playgroud)
不编译.但:
export default class MyClass {
collection = [1,2,3];
}
Run Code Online (Sandbox Code Playgroud)
请问.
错误是: error TS1192: Module '"src/app/MyClass"' has no default export.
我有两个嵌套的@Components
en angular 2.视图渲染得很好,但它总是第一次抛出一个javascript错误.这是我在Typescript中的代码.
应用HTML:
<body>
<my-app>loading...</my-app>
</body>
Run Code Online (Sandbox Code Playgroud)
应用组件:
import {bootstrap, Component} from 'angular2/angular2';
import {CanvasComponent} from "./CanvasComponent";
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>My Games</h2>
<div>
<my-canvas></my-canvas>
</div>
`,
directives: [CanvasComponent]
})
class AppComponent {
}
bootstrap(AppComponent);
Run Code Online (Sandbox Code Playgroud)
画布组件:
import {bootstrap, Component, View} from 'angular2/angular2';
@Component({
selector: 'my-canvas'
})
@View({
template: `
<div>
<span>Balls:</span>
<div>{{canvas.length}}</div>
</div>
`
})
export class CanvasComponent {
canvas = [1,2,3];
}
bootstrap(CanvasComponent);
Run Code Online (Sandbox Code Playgroud)
错误是:
EXCEPTION: The selector "my-canvas" did not match any elements
Run Code Online (Sandbox Code Playgroud) 在尝试使用ES6为我们提供的=>特性继承上下文后,我注意到这个上下文永远不会被改变.例:
var otherContext = {
a: 2
};
function foo() {
this.a = 1;
this.bar = () => this.a;
}
var instance = new foo;
instance.bar(); // returns 1
instance.bar.bind(otherContext)(); // returns 1
Run Code Online (Sandbox Code Playgroud)
没有=>运算符并使用function关键字:
function foo() {
this.a = 1;
this.bar = function () {
return this.a;
}
}
var instance = new foo;
instance.bar(); // returns 1
instance.bar.bind(otherContext)(); // returns 2
Run Code Online (Sandbox Code Playgroud)
因此,如果我们从外部调用接收函数或者只是在变量中有函数,我们怎么能确定我们是否能够将不同的函数绑定到它或者它是否只是从某个地方继承它?
javascript没有告诉你任何事情听起来很危险,人们可能因为一个非常微妙和困难的bug而陷入困境.