javascript - 在组件 Angular 2 中重定向

标签 javascript angular

我有一个简单的方法,在它结束时我想重定向到另一个组件:

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/

相关文章:

javascript - 在指令中使用相同的指令 [angularjs]

javascript - 构建客户端应用程序时,在本地存储中的键中使用前缀有什么好处? (特别是在 AngularJS/Angular 应用程序中)

javascript - 如何在 Typescript 单元测试中模拟鼠标拖动?

javascript - 无法在 OpenLayers3 中获取相对 URL

javascript - 如何根据选中的复选框禁用文本框

Javascript 查找字符串并用变量值替换

javascript - 如何检查fancybox打开与否

angular - 在Electron的透明窗口中获取阴影

javascript - Object.estimated(类似 Date 对象)未处理为 Date() 类型

angular - Angular 4 应用程序索引中的脚本文件?