Patricio Vargas
Patricio Vargas

Reputation: 5522

Different ways to do EventEmitter - Angular

I was wondering what is the difference of the following way of creating an EventEmitter, and which one is better.

@Output
balanceDueLoader = new EventEmitter<boolean>();

@Output
balanceDueLoader = new EventEmitter<boolean>(false);

@Output()
balanceDueLoader: EventEmitter<boolean> = new EventEmitter<boolean>();

Upvotes: 2

Views: 1091

Answers (2)

Gurvinder Guraya
Gurvinder Guraya

Reputation: 669

@Output balanceDueLoader = new EventEmitter();

@Output balanceDueLoader = new EventEmitter(false);

@Output() balanceDueLoader: EventEmitter = new EventEmitter();

There is no difference actually

The first one and second one is exactly same because the value is false by default. There will be difference if you pass true to the event emitter which makes the event asynchronous. By default it’s synchronous.

The only difference in third one is you are assigning it a type which will allow you ideal code completion.

https://netbasal.com/event-emitters-in-angular-13e84ee8d28c Have a look here for more details on event emitter

Upvotes: 2

csbenjamin
csbenjamin

Reputation: 356

Edit: Copying my comment here: The difference is only that you write more code. There is no difference beside that.

Original answer: When you declare a property with a initial value, generally you don't need to specify the type. The type is guessed from the initial value. So you don't need the third one. The constructor EventEmitter has a parameter with default value false, so you don't need to pass the parameter if you are gone to use the default one. So go with the first option. It has less code to write

Upvotes: 4

Related Questions