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

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.

主站蜘蛛池模板: 西和县| 广东省| 宜城市| 彰武县| 阳信县| 南部县| 措勤县| 肇庆市| 南和县| 和田县| 安乡县| 西峡县| 乌兰察布市| 枣庄市| 平凉市| 宜都市| 福清市| 德令哈市| 桂阳县| 金堂县| 平南县| 安新县| 武乡县| 勃利县| 英山县| 宜都市| 广宁县| 大厂| 柳林县| 菏泽市| 太和县| 高唐县| 揭阳市| 定安县| 洛宁县| 罗甸县| 盐亭县| 犍为县| 宁都县| 清丰县| 郎溪县|