Angular 2: Component Interaction, optional input parameters

前端 未结 3 2036
半阙折子戏
半阙折子戏 2021-02-06 20:39

I have an implementation where parent wants to pass certain data to child component via the use of @Input parameter available at the child component. However, this

3条回答
  •  我在风中等你
    2021-02-06 20:49

    Input values are optional by default. Your code will fail only when it tries to access properties of inputs that are not actually passed (since those inputs are undefined).

    You can implement OnChanges or make the input a setter instead of a property to get your code executed when a value is actually passed.

    export class child {
    
        @Input set showName(value: boolean) {
          this._showName = value;
          doSomethingWhenShowNameIsPassed(value);
        }
    
        constructor() { }
    }
    

提交回复
热议问题