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

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.

主站蜘蛛池模板: 宜章县| 昭通市| 无锡市| 静宁县| 枣庄市| 塘沽区| 墨脱县| 建瓯市| 新蔡县| 林周县| 古丈县| 荥经县| 察雅县| 长乐市| 禹州市| 宜春市| 宜阳县| 临泉县| 广德县| 伊宁县| 尖扎县| 汉寿县| 广丰县| 青田县| 平顶山市| 乌拉特中旗| 荣成市| 越西县| 达日县| 桂阳县| 土默特左旗| 乐东| 建瓯市| 高陵县| 昌都县| 依安县| 岐山县| 永仁县| 武胜县| 山东省| 石首市|