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

  • Expert Angular
  • Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
  • 183字
  • 2021-07-15 17:05:28

Metadata

A class can be turned into a component by annotating it with @Component and passing the necessary metadata, such as selector, template, or templateUrl. Angular considers a class as a component only after attaching metadata to it:

Let's revisit the BookComponent class we defined earlier. Angular does not consider this class as a component unless we annotate it. TypeScript leverages the ES7 feature by providing a way to decorate a class with metadata as follows:

@Component({ 
  selector:    'book-detail', 
  templateUrl: 'app/book.component.html' 
}) 
export class BookComponent { ... } 

Here, we have decorated the BookComponent class with @Component and attached metadata selector and templateUrl. It means that, wherever Angular sees the special <book-detail/> tag in the view, it will create an instance of BookComponent and render the view assigned to templateUrl, which is book.component.html.

A decorator provided by TypeScript is a function that takes configuration parameters that are used by Angular to create an instance of the component and render the associated view. Configuration parameters may also have information about directives and providers, which will be made available by Angular when the component is created.

主站蜘蛛池模板: 宁波市| 麻城市| 海兴县| 托克逊县| 池州市| 恩施市| 德昌县| 旬邑县| 根河市| 靖安县| 克东县| 南安市| 长乐市| 安阳县| 武陟县| 保定市| 义乌市| 临漳县| 南郑县| 左贡县| 永德县| 桂平市| 深泽县| 萨迦县| 葫芦岛市| 鹤庆县| 讷河市| 黎城县| 民县| 大同县| 马龙县| 桐城市| 涞水县| 定陶县| 宜阳县| 射洪县| 阳泉市| 榆社县| 望奎县| 竹山县| 宁南县|