我需要捕获查询字符串,但是出现错误。我使用的代码很简单,但是在浏览器中却失败了。
@Component({
selector: 'body',
viewProviders: [ ROUTER_PROVIDERS],
directives: [ CORE_DIRECTIVES, ROUTER_DIRECTIVES],
template: '<p>the param is {{foo}}</p>'
})
export class app {
constructor(params: RouteParams) {
// http://local.../?myText=Hello
this.foo = params.get('myText');
}
}
documentation对这种事情不是很清楚。它什么也没有,只是一个null
。
最佳答案
在 Angular RC1 Router 中,您需要让组件实现 OnActivate获取路由参数的接口(interface):
import { ROUTER_DIRECTIVES, Router, OnActivate, RouteSegment } from '@angular/router';
Component({
selector: 'body',
providers: [ ROUTER_PROVIDERS],
directives: [ CORE_DIRECTIVES, ROUTER_DIRECTIVES],
template: '<p>the param is {{foo}}</p>'
})
export class app implements OnActivate {
constructor() { }
routerOnActivate(currSegment: RouteSegment) {
this.foo = currSegment.getParam('myText'),
}
关于angular - RouteParams 和查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37687940/