角度2输入装饰器为什么需要?

时间:2016-12-30 13:43:49

标签: angular

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

2 个答案:

答案 0 :(得分:3)

要定义组件的输入,请使用@Input装饰器。

例如,组件需要一个用户参数来呈现有关该用户的信息:

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

因此,您需要向用户添加@Input绑定:

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

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

答案 1 :(得分:0)

[inputProp]="someValue"之类的组件模板中的绑定仅允许@Input() inputProp;或具有匹配名称的HTML元素的本机属性。

我只是猜测,但我想到了两个解释

  • 更高效的代码或代码生成 如果需要明确属性,则允许更有效的代码

  • 更好地支持工具 这允许工具检查代码中的错误,在编写模板时提供自动完成,可能还有更多

相关问题