如何在angular2中获取状态参数的值?

how to get value of stateparams in angular2?

  1. 我正在 angular2 中寻找状态参数。在 angular2 官方网站上搜索后,我找到了这个。

<a [router-link]="[ '/MyCmp', {myParam: 'value' } ]">

但我不明白如何在 class 或构造函数中获取此值 class?

  1. 在angular备忘单中有三种路由配置方法

    @RouteConfig([ { path: '/:myParam', component: MyComponent, as: 'MyCmp' }, { path: '/staticPath', component: ..., as: ...}, { path: '/*wildCardParam', component: ..., as: ...} ])

/ 和 /: 和 /* 这三个有什么区别?

您可以将 RouteParams 对象注入构造函数,然后调用 get 函数。这是 official documentation

中的示例
import {bootstrap, Component} from 'angular2/angular2';
import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/router';
@Component({directives: [ROUTER_DIRECTIVES]})
@RouteConfig([
 {path: '/user/:id', component: UserCmp, as: 'UserCmp'},
])
class AppCmp {}
@Component({ template: 'user: {{id}}' })
class UserCmp {
  id: string;
  constructor(params: RouteParams) {
    this.id = params.get('id');
  }
}

关于这三种语法,它们似乎不言自明

 { path: '/:myParam', component: MyComponent, as: 'MyCmp' },  //Route with param
  { path: '/staticPath', component: ..., as: ...},            // Fixed route
  { path: '/*wildCardParam', component: ..., as: ...}        //match any route which ends with wildCardParam

更新:从文档中查看这个plunkr http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm?p=preview

正在使用RouteParams 我观察到的一件事是 RouteParams 仅在路由本身定义了参数时可用,否则您会收到以下错误。