gpt4 book ai didi

angular - RouteParams 和查询字符串

转载 作者:行者123 更新时间:2023-12-02 13:20:30 27 4
gpt4 key购买 nike

我需要捕获查询字符串,但是出现错误。我使用的代码很简单,但是在浏览器中却失败了。

@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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com