简体   繁体   中英

angular 2 input decorator why it need?

我研究了Angular 2.我面临着将数据传输到父组件的组件的任务。在官方文档中编写时使用@输入。否,但我不明白为什么angular 2的开发人员需要它。为什么没有@ INPUT不会进行传输,为什么要添加@Input?

To define an input for a component, use the @Input decorator.

For example component needs a user argument to render information about that user:

<user-profile [user]="currentUser"></user-profile>

So, you need to add an @Input binding to user:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'user-profile',
  template: '<div>{{user.name}}</div>'
})
export class UserProfile {
  @Input() user;
  constructor() {}
}

Bindings in components template like [inputProp]="someValue" are only allowed for @Input() inputProp; or native properties of HTML elements with matching names.

I'm only guessing but 2 explanations come to my mind

  • more efficient code or code generation If properties need to be made explicit, this allows more efficient code

  • better support for tools This allows tools to check for errors in your code, provide autocompletion when writing templates, and probably more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM