Emit event through nested components in Angular2

前端 未结 3 812
猫巷女王i
猫巷女王i 2021-01-04 14:09

One can use Output() decorator to emit the event from the child component so that the parent component can catch it. What I need is to catch an event emitted by

3条回答
  •  长发绾君心
    2021-01-04 14:30

    If you find you have to pass certain events through make sure to be consistent and come up with a naming strategy. For instance I like to add raise to the beginning of the method name if the only thing that method does is to re-raise an event.

    eg.

    (paypalTokenized)="raisePaypalTokenized($event)"
    

    and in the ts file:

    @Output('paypalTokenized')
    public paypalTokenizedEmitter = new EventEmitter();
    
    // PayPal tokenized
    public raisePaypalTokenized(payload: PayPalTokenizedPayload)
    {
        // no other logic here!
        this.paypalTokenizedEmitter.emit(payload);
    }
    

    This means I can tell immediately that the event is being passed through with no further action.

提交回复
热议问题