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

查看:73
本文介绍了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.

其中一个是无法导入的RouteParams来自@ angular / router。当我尝试使用@ 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:


ORIGINAL EXCEPTION:没有RouteParams的提供者!

ORIGINAL EXCEPTION: No provider for RouteParams!

app.component:

app.component:

@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天全站免登陆