[英]angular2: routing by code after Observable success
我知道使用routerLink
指令可以路由到目的地:
<a [routerLink]=" ['/app'] ">LogOut</a>
這是在您的網站上創建鏈接的靜態方法。
但是,有時必須根據是否正確處理了http
請求來完成路由:
public createAccount():void {
this.clearAlerts();
this.commty.create(this.form.value.mail, this.form.value.passwd)
.subscribe(
(result: any) => { // <-- handler for result
>>>>>>>>>> go to '/app' route <<<<<<<<<<<<<<<
},
(error: any) => { // <-- handler for error
this.addAlert(error.message);
}
);
}
我想一定有一種使用打字稿代碼來做到這一點的方法...您能給我一個例子還是任何文檔?
router.navigate(['/app'])
但為此,您需要在組件中導入和注入Router
import {Router} from '@angular/router';
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello</h1>',
})
export class AppComponent {
constructor(private router: Router){
router.navigate(['/app'])
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.