Cristiano Fontes Cristiano Fontes - 1 month ago 13
Javascript Question

Why the need for () when importing services in Angular 2?

I am studying angular 2 and have found this statement disturbing.


Don't forget the parentheses! Neglecting them leads to an error that's
difficult to diagnose.


While reading this

Why is there a parenteses there what is it's function? Why have it there in the first place if it causes "hard to throubleshoot" problems?

Cheers!

Answer

These parens are not related to imports but to decorators

@Injectable() // correct
export class MyService {...}

vs

@Injectable // wrong
export class MyService {...}

because a function reference is quite different from a function invocation.