我有一个简单的方法,在它结束时我想重定向到另一个组件:
export class AddDisplay{
display: any;
addPairTo(name: string, pairTo: string){
this.display = {};
this.display.name = name;
this.display.pairTo = pairTo;
}
}
我想做的是在方法结束时重定向到另一个组件:
export class AddDisplay{
display: any;
addPairTo(name: string, pairTo: string){
this.display = {};
this.display.name = name;
this.display.pairTo = pairTo;
this.redirectTo('foo');
}
}
我如何在 Angular 2 中实现这一点?
最佳答案
首先配置路由
import {RouteConfig, Router, ROUTER_DIRECTIVES} from 'angular2/router';
和
@RouteConfig([
{ path: '/addDisplay', component: AddDisplay, as: 'addDisplay' },
{ path: '/<secondComponent>', component: '<secondComponentName>', as: 'secondComponentAs' },
])
然后在你的组件中导入然后注入(inject)Router
import {Router} from 'angular2/router'
export class AddDisplay {
constructor(private router: Router)
}
你要做的最后一件事就是打电话
this.router.navigateByUrl('<pathDefinedInRouteConfig>');
或
this.router.navigate(['<aliasInRouteConfig>']);
关于javascript - 在组件 Angular 2 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32896407/