<a [router-link]="[ '/MyCmp', {myParam: 'value' } ]">
但我无法理解如何在类或构造函数中获取此值 班级?
在角度备忘单中,路由配置有三种方法就是
@RouteConfig([
{ path: '/:myParam', component: MyComponent, as: 'MyCmp' },
{ path: '/staticPath', component: ..., as: ...},
{ path: '/*wildCardParam', component: ..., as: ...}
])
这三者之间有什么区别:/和/:和/ *?
答案 0 :(得分:4)
您可以将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仅在路径本身定义了参数时才可用,否则你会得到以下错误。