Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 forwardRef?

I bought an Angular 4 template mostly to see how the layout is being done but includes working components, I noticed they have a bunch of settings in the app component which is mostly for the menu, changing styles, mostly controlling the outer framework.

In the menu component and few other components they are communicating with the AppComponent settings using the following:

constructor(@Inject(forwardRef(() => AppComponent)) public app:AppComponent) {}

and will call something like: this.app.darkMenu = true in the menu component

Is this valid design, good, bad, outdated? I didn't even know you could communicate with the App or parent component like this? Should this be an Observable Subject or EventEmitter instead or is this acceptable to communicate via forwardRef? This seems that is similar in .NET where I could communicate with the MasterPage by using this.master.whateverproperty.

I like how forwardRef works but not sure if I should use it or change it to communicate differently??

like image 821
Fab Avatar asked Jul 13 '17 14:07

Fab


1 Answers

forwardRef is not for communication. forwardRef is to be able to use a type name that is only declared further down in the same file.
If export class AppComponent {} is in a different file, there is no need for forwardRef.
IMHO it's usually better to use a shared service for communication between components that are not direct parent-child.

like image 156
Günter Zöchbauer Avatar answered Oct 22 '22 00:10

Günter Zöchbauer