【问题标题】:angular 2 input decorator why it need?angular 2 输入装饰器为什么需要它?
【发布时间】:2016-12-30 21:47:45
【问题描述】:

我学习Angular 2。我面临将数据传输到父组件的组件的任务。在官方文档中编写使用@输入。不但是我不明白为什么Angular 2的开发人员会这样做需要吗?。为什么没有@INPUT 就不会进行转移。为什么要添加@Input?在解释的文档中我不清楚

【问题讨论】:

标签: angular


【解决方案1】:

要为组件定义输入,请使用 @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() {}
}

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 2012-03-22
      • 2018-04-17
      • 2010-09-21
      • 2011-01-02
      • 1970-01-01
      • 2019-01-18
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多