【问题标题】:How to set default values for Angular 2 component properties?如何为 Angular 2 组件属性设置默认值?
【发布时间】:2016-07-04 11:21:52
【问题描述】:

在编写 Angular 2.0 组件时,如何设置属性的默认值?

例如 - 我想默认将 foo 设置为 'bar',但绑定可能会立即解析为 'baz'。这如何在生命周期挂钩中发挥作用?

@Component({  
    selector: 'foo-component'
})
export class FooComponent {
    @Input()
    foo: string = 'bar';

    @Input()
    zalgo: string;

    ngOnChanges(changes){
          console.log(this.foo);
          console.log(changes.foo ? changes.foo.previousValue : undefined);
          console.log(changes.foo ? changes.foo.currentValue : undefined);
    }
}

鉴于以下模板,这是我期望的值。我错了吗?

<foo-component [foo] = 'baz'></foo-component>

登录到控制台:

'baz'
'bar'
'baz'
<foo-component [zalgo] = 'released'></foo-component>

登录到控制台:

'bar'
undefined
undefined

【问题讨论】:

  • 当你尝试它会发生什么?
  • @BryanRayner 当前控制台的打印方式是正确的。您面临的问题是什么?
  • 我目前没有遇到问题,只是寻求对预期行为的澄清。当我找不到我的好奇心的答案时,我决定问这个问题,以防其他人也有同样的清晰愿望。
  • 在您的示例中,您缺少 @Input() 上的括号
  • 对于那些仍在寻找答案的人,在 Angular 官方文档中有“输入设置器强制”,您可以在其中使用输入的获取器和设置器。 angular.io/guide/template-typecheck#input-setter-coercion

标签: angular typescript


【解决方案1】:

这是一个有趣的话题。 您可以使用两个生命周期钩子来弄清楚它是如何工作的:ngOnChangesngOnInit

基本上,当您将默认值设置为 Input 时,这意味着它只会在该组件上没有值的情况下使用。 并且有趣的部分会在组件被初始化之前被改变。

假设我们有这样的组件,其中包含两个生命周期钩子和一个来自 input 的属性。

@Component({
  selector: 'cmp',
})
export class Login implements OnChanges, OnInit {
  @Input() property: string = 'default';

  ngOnChanges(changes) {
    console.log('Changed', changes.property.currentValue, changes.property.previousValue);
  }

  ngOnInit() {
    console.log('Init', this.property);
  }

}

情况1

html 中包含的组件没有定义property

结果我们将在控制台中看到: Init default

这意味着onChange 未被触发。初始化被触发,property 的值是default,正如预期的那样。

情况2

组件包含在 html 中并设置了属性 &lt;cmp [property]="'new value'"&gt;&lt;/cmp&gt;

结果我们将在控制台中看到:

Changednew valueObject {}

Initnew value

这个很有趣。首先触发onChange钩子,将property设置为new value,之前的值为empty object!并且只有在那之后onInit 钩子才被property 的新值触发。

【讨论】:

  • 此行为是否有任何指向官方文档的链接?了解这背后的逻辑和推理会很好,还可以跟踪每个版本的行为。
  • 我没有看到这样的信息,以上都是我自己的调查。如果您阅读编译好的 js 文件,我想您可以找到更多答案
  • 我正在寻找有关具有默认值的 @Input 的文档。 @slicepan 有一个指向组件生命周期文档的链接,但我没有看到文档中使用的默认值。
  • @nycynik 只需将其用作默认值:@Input() someProperty = 'someValue';
  • 你是救星。在从 AngularJS 应用程序升级到 Angular 7.x 时,这让我很头疼
【解决方案2】:

这是最好的解决方案。 (角度所有版本)

解决方案为@Input 变量设置默认值。如果没有向该输入变量传递值,那么它将采用默认值

我已经为此类类似问题提供了解决方案。你可以从here找到完整的解决方案

export class CarComponent implements OnInit {
  private _defaultCar: car = {
    // default isCar is true
    isCar: true,
    // default wheels  will be 4
    wheels: 4
  };

  @Input() newCar: car = {};

  constructor() {}

  ngOnInit(): void {

   // this will concate both the objects and the object declared later (ie.. ...this.newCar )
   // will overwrite the default value. ONLY AND ONLY IF DEFAULT VALUE IS PRESENT

    this.newCar = { ...this._defaultCar, ...this.newCar };
   //  console.log(this.newCar);
  }
}

【讨论】:

  • 这意味着 {} 的默认 @Input() 值永远不会被使用,对吧?你不妨跳过它:@Input() newCar: car;
  • “最好的”是相对的。您正在设置默认值 yes 但是当输入更改时您不会再次设置它。
猜你喜欢
  • 2017-03-27
  • 2019-09-24
  • 1970-01-01
  • 2017-06-11
  • 2010-10-28
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多