我有一个简单的方法,最后我想重定向到另一个组件:

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中实现这一点?

推荐答案

首先配置路由

imp或t {RouteConfig, Router, ROUTER_DIRECTIVES} from 'angular2/router';

@RouteConfig([
  { path: '/addDisplay', component: AddDisplay, as: 'addDisplay' },
  { path: '/<secondComponent>', component: '<secondComponentName>', as: 'secondComponentAs' },
])

then in your component imp或t 和 then inject Router

imp或t {Router} from 'angular2/router'

exp或t class AddDisplay {
  construct或(private router: Router)
}

你要做的最后一件事就是打电话

this.router.navigateByUrl('<pathDefinedInRouteConfig>');

this.router.navigate(['<aliasInRouteConfig>']);

Angular相关问答推荐

根据Angular中的构建来卸载独立组件

使用多个管道时Angel 17空数据

Angular 17上的material 工具提示的自定义样式

[已解决]如何在模块ngDoBootstrap中测试Angular 自定义元素和做覆盖

Angular 17不接受带点(.)的路径在开发环境中,它直接抛出一个404错误

当元素在元素上使用迭代变量时,如何重构ngFor?

确保我的角库S服务构建提供独立的友好提供功能

AOS-如何在向上滚动时不再次隐藏元素

如何在 Angular 库中将依赖项声明为可选?

如何访问父组件中提供的 TemplateRef 内的服务?

我想将一个对象传递给一个子组件.我做了一切,但没有任何效果. (Angular )

ng14中具有强类型表单的FormBuilder

Angular:将间隔与增量和减量相结合

无法绑定到matDatepicker,因为它不是 input的已知属性

为什么Angular 12 'ng serve' 构建应用程序的速度很慢?

如何提高 Angular2 应用程序的加载性能?

Angular 2 - 全局 CSS 文件

提交后在Angular 2中重置表单

No provider for Router?

Angular 5 和material - 如何从 SnackBar 组件更改背景 colored颜色