我有一個簡單的Angular.io應用程序。 (angular-cli/4.1.0)Angular.io/Angular 4.如何在觸發另一個組件視圖時刷新一個組件視圖
我有一個呈現用戶名的NavbarComponent。
第一次訪問應用程序時,我第一次沒有登錄,我的應用程序重定向到LoginComponent。我的NavBar也呈現,但沒有用戶名。成功登錄後,我被重定向到我的HomeComponent。
這是問題所在。我的NavBar不顯示用戶名。但如果我做了刷新/ Ctrl + R的用戶名呈現。
出了什麼問題?
app.component.html
<nav-bar></nav-bar>
<router-outlet></router-outlet>
navbar.compoment.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nav-bar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
me;
ngOnInit() {
this.me = JSON.parse(localStorage.getItem('currentUser'));
}
}
login.component.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertService, AuthenticationService } from '../_services/index';
@Component({
moduleId: module.id,
templateUrl: 'login.component.html'
})
export class LoginComponent implements OnInit {
model: any = {};
loading = false;
returnUrl: string;
constructor(
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService,
private alertService: AlertService) { }
ngOnInit() {
// reset login status
this.authenticationService.logout();
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
login() {
this.loading = true;
this.authenticationService.login(this.model.email, this.model.password)
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
this.alertService.error(error);
this.loading = false;
this.errorMsg = 'Bad username or password';console.error('An error occurred', error);
});
}
}
謝謝AJT_82和@JusMalcolm。我會在這裏嘗試您的建議,我認爲您提到的共享服務可能是「最佳實踐」方式。不過,我認爲沒有一個更實用的方法來做到這一點是令人遺憾的。您應該能夠調用一種區域刷新或app.module刷新來查看路線並重新加載路線中的所有組件。 – ChrisWorks
感謝提供rxjs的人! –