组件Angular 2中的重定向 [英] Redirect within component Angular 2

查看:95
本文介绍了组件Angular 2中的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple method that at the end of it I want to redirect to another component:

export class AddDisplay{
  display: any;

  addPairTo(name: string, pairTo: string){
    this.display = {};
    this.display.name = name;
    this.display.pairTo = pairTo;

  }
}

我想做的是在方法结束重定向到另一个组件:

What I wanna do is at the end of the method redirect to another component:

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

How do I achieve this in Angular 2?

推荐答案

首先配置路由

import {RouteConfig, Router, ROUTER_DIRECTIVES} from 'angular2/router';

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

然后在你的组件导入然后注入路由器

then in your component import and then inject Router

import {Router} from 'angular2/router'

export class AddDisplay {
  constructor(private router: Router)
}

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

the last thing you have to do is to call

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

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

这篇关于组件Angular 2中的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆