Этот ngOnInit не выполняет строку console.log
ngOnInit() {
this.loadBlogPosts();
this.authStatusService.getAuthUsername().subscribe(
status => {
// got username status
console.log("This is the username status from home", status);
this.authUsername = status;
}
);
}
Эта строка выполняется
this.loadBlogPosts();
Эта строка не выполняется:
console.log("This is the username status from home", status);
Это app-module.ts.
const routes: Routes = [
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'add', component: BlogPostAddEditComponent },
{ path: 'posts', component: BlogPostsComponent },
{ path: 'post', component: BlogPostComponent },
{ path: 'auth', component: AuthComponent },
{ path: 'blogpost/:id', component: BlogPostComponent },
{ path: '**', redirectTo: '/' }
];
Это app.component. html
<app-headernav></app-headernav>
<router-outlet></router-outlet>
Это услуга:
export class AuthStatusService {
authUsernameChange: Subject<string> = new Subject<string>();
constructor() { }
public getAuthUsername(): Observable<string> {
return this.authUsernameChange.asObservable();
}
public setAuthUsername(user: string) {
this.authUsernameChange.next(user);
}
}