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

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.

主站蜘蛛池模板: 太和县| 申扎县| 武陟县| 咸阳市| 巴南区| 五莲县| 藁城市| 西乡县| 翁牛特旗| 商南县| 汽车| 江永县| 石阡县| 鸡泽县| 蛟河市| 陈巴尔虎旗| 北京市| 建瓯市| 郑州市| 内乡县| 凤凰县| 仁布县| 榆林市| 南部县| 连平县| 琼结县| 九龙县| 陆丰市| 沁阳市| 苏尼特右旗| 全南县| 呼图壁县| 门头沟区| 万山特区| 凤凰县| 长宁区| 汉沽区| 巨野县| 阜平县| 越西县| 垦利县|