如何在Angular2应用程序中获取参数?

如何在Angular2应用程序中获取参数?,angular,typescript,Angular,Typescript,在Angular2中,如何获取参数并像在Php中的会话一样将其存储在本地 在继续导航到正在调用安全Rest Web服务的仪表板组件之前,我需要获取access_令牌。(需要代币) 并删除app.components.ts中的提供程序 但一个错误正在发生: Cannot resolve all parameters for 'RouteParams'(?). Make sure that all the parameters are decorated with Inject or have v

在Angular2中,如何获取参数并像在Php中的会话一样将其存储在本地

在继续导航到正在调用安全Rest Web服务的仪表板组件之前,我需要获取access_令牌。(需要代币)

并删除app.components.ts中的提供程序

但一个错误正在发生:

Cannot resolve all parameters for 'RouteParams'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'RouteParams' is decorated with Injectable.
angular2-polyfills.js:322 Error: TypeError: Cannot read property 'getOptional' of undefined(…)ZoneDelegate.invoke @ angular2-polyfills.js:322Zone.run @ angular2-polyfills.js:218(anonymous function) @ angular2-polyfills.js:567ZoneDelegate.invokeTask @ angular2-polyfills.js:355Zone.runTask @ angular2-polyfills.js:254drainMicroTaskQueue @ angular2-polyfills.js:473ZoneTask.invoke @ angular2-polyfills.js:425
angular2.dev.js:23740 EXCEPTION: No provider for RouteParams! (AppComponent -> RouteParams)
编辑2:INATTION错误,新建main.ts: (这次只有一个引导><)

现在只有这个错误:

Cannot resolve all parameters for 'RouteParams'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'RouteParams' is decorated with Injectable.
angular2-polyfills.js:322 Error: TypeError: Cannot read property 'getOptional' of undefined(…)

更新不推荐使用的路由器

仅在
bootstrap()
AppComponent
中添加这些提供程序

bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    HTTP_PROVIDERS])
把它们移到其他地方。如果应该与整个应用程序共享相同的提供者,则不需要多次提供它们

还要确保从
angular2/ROUTER
导入
RouteParams
ROUTER\u提供程序
。它们不是通过
angular2/core
导出的

另见我对这一问题的答复

在根组件中,您可以注入路由器并订阅路由事件,然后从路由器获取参数,如

export class AppComponent {
  constructor(private router:Router) {
    router.subscribe(route => {
      console.debug(this.router.currentInstruction.component.params);
    });
  }
}
在路由器添加的组件上,您可以注入
RouteParams
like并访问以下值

export class Other{
    constructor(private routeParams: RouteParams) {
    console.debug(this.routeParams);
    console.log(this.routeParams.get('filter_industry'));
    console.log(this.routeParams.get('filter_start_with'));
  }
}

this.token2=\u routeParams.get('access\u token');这部分:)在
bootstrap()
中是否也有这些提供程序,或者仅在根组件上有这些提供程序,`
,指令:[ROUTER\u directives],提供程序:[ROUTER\u providers,HTTP\u providers,HeroService,RouteParams]})
?我有:引导程序(AppComponent,[HTTP\u providers]);在my main.ts中,但即使使用:bootstrap(AppComponent,[HTTP_PROVIDERS,ROUTER_PROVIDERS]);我也有同样的错误:/Looks like()您实际上没有两行
bootstrap(AppComponent)
行,如您的问题所示?(编辑:1),是吗?是我干的。同样的错误,从“angular2/ROUTER”导入{ROUTER_PROVIDERS,RouteParams};在“main.ts”文件中
bootstrap(AppComponent, [ROUTER_PROVIDERS,HTTP_PROVIDERS,RouteParams]);
Cannot resolve all parameters for 'RouteParams'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'RouteParams' is decorated with Injectable.
angular2-polyfills.js:322 Error: TypeError: Cannot read property 'getOptional' of undefined(…)
bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    HTTP_PROVIDERS])
export class AppComponent {
  constructor(private router:Router) {
    router.subscribe(route => {
      console.debug(this.router.currentInstruction.component.params);
    });
  }
}
export class Other{
    constructor(private routeParams: RouteParams) {
    console.debug(this.routeParams);
    console.log(this.routeParams.get('filter_industry'));
    console.log(this.routeParams.get('filter_start_with'));
  }
}