官术网_书友最值得收藏!

How is it possible?

Why did not we do it before, in Angular 1? To make AOT work the application has to have a clear separation of the static and dynamic data in the application. And the compiler has to built in such a way that it only depends on the static data. When designing and building Angular we put a lot of effort to do exactly that. And such primitives as classes and decorators, which the new versions of JavaScript and TypeScript support, made it way easier.

To see how this separation works in practice, let's look at the following example. Here, the information in the decorator is known statically. Angular knows the selector and the template of the talk component. It also knows that the component has an input called talk and an output called rate. But the framework does not know what the constructor or the onRate function do.

@Component({
selector: 'talk-cmp',
template: `
{{talk.title}} {{talk.speaker}}
Rating: {{ talk.rating | formatRating }}
<watch-button [talk]="talk"></watch-button>
<rate-button [talk]="talk" (click)="onRate()"></rate-button>
`
})
class TalkCmp {
@Input() talk: Talk;
@Output() rate: EventEmitter;


constructor() {
// some initialization logic
}

onRate() {
// reacting to a rate event
}
}

Since Angular knows all the necessary information ahead of time, it can compile this component without actually executing any application code, as a build step.

主站蜘蛛池模板: 西吉县| 无为县| 漾濞| 赫章县| 和林格尔县| 宿州市| 乐都县| 泽普县| 纳雍县| 神农架林区| 南澳县| 嫩江县| 潞西市| 鹤岗市| 武夷山市| 甘泉县| 四平市| 长顺县| 五大连池市| 广汉市| 遵化市| 扶绥县| 济阳县| 永仁县| 大方县| 休宁县| 祁阳县| 马尔康县| 仲巴县| 贵州省| 澄江县| 宜章县| 乃东县| 黔江区| 巴南区| 和龙市| 成都市| 阜平县| 于田县| 通许县| 嘉黎县|