Angular 2 rc1 中的 RouteParams [英] RouteParams in Angular 2 rc1

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

问题描述

我从测试版开始就一直在尝试 Angular 2,现在有了 rc.0+,有些事情发生了变化.

I've been trying out Angular 2 since beta, and now with rc.0+ some things have changed.

其中之一是无法从@angular/router 导入的 RouteParams.当我尝试使用 @angular/router-deprecated 时,我收到一条错误消息:

One of those are RouteParams which cannot be imported from @angular/router. And when I try with @angular/router-deprecated I get an error message:

原始例外:没有提供 RouteParams!

ORIGINAL EXCEPTION: No provider for RouteParams!

应用程序组件:

@Routes([
  { path: '/', component: StartComponent },
  {path: '/:projId/:userName', component: ProjectListComponent},
  { path: '*', component: StartComponent },
])

project-list.component:

project-list.component:

import {Component, OnInit} from '@angular/core';
import {RouteParams} from '@angular/router-deprecated';

@Component({
  ...
})
export class ProjectListComponent implements OnInit {
  userName:string;
  projId:string;

  constructor(private params:RouteParams) {
    this.userName = params.get('userName');
    this.projId = params.get('projId');
  }
}

我现在可以从哪里导入 RouteParams,还是我做错了什么?

Where can I import the RouteParams from now, or is it something else I'm doing wrong?

谢谢!

推荐答案

一种方法是

  routerOnActivate(curr: RouteSegment) {
    this.userName = curr.getParam('userName');
    this.projId = curr.getParam('projId');
  }

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

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