我是angular 2.的新手。我理解import語句意味着我們在此文件中包含組件類。@ Component在angular2中做了什麼
我的問題是誰有解釋爲什麼我們需要@Component註釋它究竟做了什麼?
import {component} from '@angular2/core';
@component
{
}
export class MyComponent
{
}
我是angular 2.的新手。我理解import語句意味着我們在此文件中包含組件類。@ Component在angular2中做了什麼
我的問題是誰有解釋爲什麼我們需要@Component註釋它究竟做了什麼?
import {component} from '@angular2/core';
@component
{
}
export class MyComponent
{
}
它增加了元數據爲MyComponent這實際上使得類組件 沒有它的類的類將是一個正常的打字稿類
進一步的信息在這裏:http://blog.thoughtram.io/angular/2015/05/03/the-difference-between-annotations-and-decorators.html
的@Component
註釋是Typescript decorator。裝飾器是一種特殊的聲明,可以附加到類聲明,方法,訪問器,屬性或參數。
Angular的組件註解應用了所需的框架構建塊,使其成爲Angular2中的一個可用組件。更具體地說:
@Component
註釋指定組件何時實例化以及綁定哪些屬性和主監聽器。
見你能想到的@Component作爲恭維的 類的構造函數,並且註釋將您的類成角狀的部件實際。
Angular在內部解析@Component註釋(以及所有註釋)並生成額外的代碼,將請求的行爲添加到類中。
如果你看一下你的類的transpiled代碼,你可以看到什麼呢角與@Component註解,財產以後這樣的:
MyComponent = (function() {
function MyComponent() {
...
}
...