2017-08-01 70 views
1

我在Angular 2應用程序上有這樣的組件,但它沒有在Angular 4上運行,RouteSegment不起作用。如何在Angular 4上做到這一點?routerOnActivate on Angular 4

export class CallBackLoginComponent { 

private user: User = new User(); 
private showLoading: boolean = false; 
private errorMessage: string = null; 

constructor(private _loginService: LoginService, 
    private _router: Router, 
    private userService: UserService, 
    private _comunication: Comunication, 
    private _routerParams: RouteSegment) { 
}; 

//Parametros recebidos atraves da rota /user/token 
routerOnActivate(curr: RouteSegment) { 
    var token = {token: curr.getParam('token')}; 
    if (token != null) { 
     this.showLoading = true; 
     this.errorMessage = null; 

     this.userService.vldToken(token).subscribe(
      result => this.onLoginResult(result), 
      error => this.onLoginError(error) 
     ); 
    } 
    else{ 
     this._router.navigate(['/']); 
    } 
} 
+0

a2.hubwiz.com/docs/ts/latest/api/router/OnActivate-interface.html做發佈 – Sreemat

+0

這看起來更像角2預發佈之前,一些搜索?如果是這樣,路由器會在Angular 2的預發佈/ beta/rc和它的發佈版本之間進行更改。我不相信在Angular 4中這一切都發生了變化。 – DeborahK

+0

是的,它是Angular 2的一個預發售版 – Undercrazy

回答

0

該版本的路由器曾用於早期版本的候選版本。在RC 4和RC5之間有一個重要的路由器重寫(更新到版本3)。

現在您將不得不訂閱route.params並獲取您要查找的關鍵標記。

export class CallBackLoginComponent implements OnInit { 

    private user: User = new User(); 
    private showLoading: boolean = false; 
    private errorMessage: string = null; 

constructor(private _loginService: LoginService, 
    private _route: ActivatedRoute, 
    private _router: Router, 
    private userService: UserService, 
    private _comunication: Comunication) { 
}; 

ngOnInit() { 
    this._route.params().subscribe(params => this.routerOnActivate(params['token']); 
} 

//Parametros recebidos atraves da rota /user/token 
routerOnActivate(token: string) { 
    if (token != null) { 
     this.showLoading = true; 
     this.errorMessage = null; 

     this.userService.vldToken(token).subscribe(
      result => this.onLoginResult(result), 
      error => this.onLoginError(error) 
     ); 
    } 
    else{ 
     this._router.navigate(['/']); 
    } 
} 
+0

親愛的,謝謝你的幫助。看,我可以使用類似ActivatedRoute類型的route.params嗎? – Undercrazy

+0

當然!我用適當的代碼編輯我的答案 –

+0

另一種方法做 on ngOnInit() const token = this._route.snapshot.params.token 令牌將傳遞參數在您的路線 – Ricardo