Routing 角度2错误:无法解析';路线图';

Routing 角度2错误:无法解析';路线图';,routing,angular,Routing,Angular,试图使用RouteParams获取查询字符串参数,但我只得到了错误 无法解析“RouteParams”(?)的所有参数。确保所有 参数用Inject修饰或具有有效类型 注释,“RouteParams”用可注入的。 angular2 polyfills.js:332错误:无法读取的属性“getOptional” 未定义 看看这个。如何使用RouteParams而不收到此警告 重要文件包括: boot.ts: import {bootstrap} from 'angular2/platform/br

试图使用RouteParams获取查询字符串参数,但我只得到了错误

无法解析“RouteParams”(?)的所有参数。确保所有 参数用Inject修饰或具有有效类型 注释,“RouteParams”用可注入的。 angular2 polyfills.js:332错误:无法读取的属性“getOptional” 未定义

看看这个。如何使用RouteParams而不收到此警告

重要文件包括:

boot.ts

import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS, RouteParams} from 'angular2/router';
import {AppComponent} from './app/app.component';

bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);
import {Component, OnInit} from 'angular2/core';
import {RouteParams} from "angular2/router";

@Component({
  selector: 'app',
  template: `
    <p *ngIf="guid">guid gived is: {{guid}}</p>
    <p *ngIf="!guid">No guid given in query-string in URL</p>
  `
})
export class AppComponent implements OnInit {
  guid:string;

  constructor(private _params: RouteParams) {} //

  ngOnInit() {
    this.guid = this._params.get('guid');
  }

}
app.component.ts

import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS, RouteParams} from 'angular2/router';
import {AppComponent} from './app/app.component';

bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);
import {Component, OnInit} from 'angular2/core';
import {RouteParams} from "angular2/router";

@Component({
  selector: 'app',
  template: `
    <p *ngIf="guid">guid gived is: {{guid}}</p>
    <p *ngIf="!guid">No guid given in query-string in URL</p>
  `
})
export class AppComponent implements OnInit {
  guid:string;

  constructor(private _params: RouteParams) {} //

  ngOnInit() {
    this.guid = this._params.get('guid');
  }

}

但是我仍然得到相同的错误(更新了).

您是否在@RouteConfig中指定了
guid

@RouteConfig([
  {path: '/something/:guid', name: 'Something', component: Something}
])

从中删除
RouteParams

bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);
RouteParams
由路由器提供。如果你自己提供,它就会失败

有关示例,请参见
RouteParams
只能注入路由器添加的组件


我不能正确理解您的问题,但正如您在评论中所说的是

是否有其他获取url查询参数的方法

我们可以使用
数据
属性发送
我们可以使用angualr2提供的
@RouteConfig注释
data
属性通过路由发送数据。通过使用此属性,我们可以在路由配置时向组件发送附加数据,而无需在URL中显示。下面是该属性的示例

@RouteConfig([
    {path: '/product/:id', component: ProductDetailComponentParam,
     as: 'ProductDetail', data: {isProd: true}}])

export class ProductDetailComponentParam {
    productID: string;
    constructor(params: RouteParams, data: RouteData) {
        this.productID = params.get('id');

        console.log('Is this prod environment', data.get('isProd'));
    }
}
通过使用此选项,我们可以通过路由发送数据,而无需在URL中显示。

欲了解更多信息,请阅读此

另见-


在看到正确且被接受的甘特答案后,我想添加我自己的答案(我问了最初的问题)

对于我的情况来说,这真是太过分了:我没有路由(我只有一个单页单页应用程序),所以我必须:

  • 引入另一个“外部”组件(RouteConfig)
  • 使用路由器配置和路由器基础模板创建专用组件(使用
  • 在要使用查询字符串参数的组件中注入RouteParams
最后我可以查询字符串参数

在我的例子中(只有一个参数),我只做了一行(为便于阅读,分为4行):


即使我没有任何路由(除了默认的根路由(不使用路由器时这是一个路由))?有没有其他方法获取url查询参数?我不这么认为。。。routeParams需要从RouteConfig进行配置,如Angular 1。您可以添加
useAsDefault:true
选项来指定默认根路由。我添加了@RouteConfig,仍然会得到相同的错误(请参阅相同的plunker)。我也用这个更新了问题…PS:-
因为在angular2测试版中
已更改为
名称
。如果我这样做,我会得到另一个错误:
异常:没有RouteParams的提供程序您可以编辑plunker并使其工作吗?我从引导(和导入)中删除了两个路由器\u提供程序、RouteParams,并在组件上添加了
提供程序:[RouteParams]
,但得到了相同的错误…这个
guid
来自何处?如果您没有任何路由?它只是我的应用程序需要的一个查询字符串,所以有人会这样使用它:mydomain.com/myapp?guid=something。也许可以在这里叫它其他的东西,比如“TestParameter”或其他什么…我刚刚发布了我的答案,希望能对你有所帮助。我感兴趣的是获取参数(从url查询字符串),而不是设置…从我没有理解你的观点的地方获取抱歉。你只有一个路由器比从哪里可以得到参数?