最小changes..best實踐緊密結合,你必須創建一個登錄電子組件和路由配置這樣 出口常量路線定義登錄的最佳方式:RouterConfig = [
{path: 'login', component: LoginComponent},
{path: '', component: LoginComponent}] //default to login page
登錄組件。我的代碼就像
export class LoginComponent implements OnInit {
private jwtHelper:JwtHelper = new JwtHelper();
messages:String[] = [];
localUser = {
username: '',
password: ''
}
constructor(private _service:LoginService, private _router:Router) {
}
login() {
this._service.login(this.localUser).then((data) => {
if (data) {
this._router.navigate(['/companies']);
}
},
(error) => {
this.messages = error;
});
}
clearfields() {
this.localUser.username = '';
this.localUser.password = '';
this.messages = [];
}
ngOnInit():any {
if (window.localStorage.getItem('auth_key') === undefined) {
console.log("window.localStorage.getItem('auth_key'): " + window.localStorage.getItem('auth_key'));
}
else if (window.localStorage.getItem('auth_key') != null && !this.jwtHelper.isTokenExpired(window.localStorage.getItem('auth_key'))) {
this._router.navigate(['/companies']);
}
}
登錄後,你可以瀏覽到自卑您的需要
請加errormessages和相關代碼片段(如路由器配置等)的任何頁面 – jbin