Continuing from this question Angular 4^ : How to have more than one child of a component with each child targeting its own router outlet, I'm able to have some child components injected into multiple parent components, now I want to pass data from those parents, async, to child. Tried @Input, can't seem to win.
Child
export class UserheaderComponent implements OnInit, AfterViewInit, OnChanges {
loading;
@Input() data;
user = {
name: '______________',
icon: '',
username: '_________',
uid: '________'
};
constructor(private router: Router) {
}
goToUser(uid) {
this.router.navigate(['user'], { queryParams: { uid: uid } });
}
ngOnInit() {
this.user = this.data;
console.log(this.data);
}
ngAfterViewInit() {
console.log(this.data);
}
ngOnChanges(changes: SimpleChanges) {
console.log(changes);
}
}
Parent Html
<router-outlet name='userprofile-userhead' [data]="currentUser"></router-outlet>
Parent TS
export class UserprofileComponent {
public currentUser;
constructor(
private userFactory: UserFactory,
private router: Router,
private snack: MatSnackBar) {
this.userFactory.checkSession(exists => {
if (!exists) {
return;
}
this.userFactory.getSessionUser((uid, user) => {
this.currentUser = user;
});
});
}
}
AND ROUTING
path: '', component: UserprofileComponent, outlet: 'userprofile', children: [
{ path: '', component: UserheaderComponent, outlet: 'userprofile-userhead' },
]
Nothing at all gets passed to child, is this possible with this kind of arrangement or I'm missing something?
Can't use a shared service.
Every component should use this with its own Id. Imagine this is in a timeline of posts like context, like a social media timeline, and this is a head of the post, you know, where the user icon, name...username is. So a 'post' component will inject this as a child, pass it a user object:{name:'...',username:'...'}
, so I don't see how a service will do here.
Now while we at that, somewhere on the app, a profile component, a search component might call this...
If you still think a service will do, please elaborate.