尝试使用RouteParams来获取查询字符串参数,但我只是得到了错误
无法解析'RouteParams'(?)的所有参数.确保所有参数都使用Inject进行修饰或具有有效的类型注释,并且'RouteParams'使用Injectable进行修饰.angular2-polyfills.js:332错误:无法读取未定义的属性'getOptional'.......
看看这个Plnkr.如何在不收到此警告的情况下使用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]);
Run Code Online (Sandbox Code Playgroud)
并且app.component.ts:
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');
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
我没有任何路由,我只有默认的根路由(如果我没有其他路由,那么可以称之为路由......).但正如Gianluca建议的那样,我添加了以下路由配置:
@RouteConfig([
{ path: '/:guid', name: 'Home', component: AppComponent, useAsDefault: true },
])
Run Code Online (Sandbox Code Playgroud)
但我仍然得到同样的错误(Plnkr更新)......
Gün*_*uer 10
RouteParams从中移除
bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);
Run Code Online (Sandbox Code Playgroud)
RouteParams由路由器提供.如果您自己提供注入失败.
有关示例,请参阅https://angular.io/api/router/Params.RouteParams只能注入路由器添加的组件.
| 归档时间: |
|
| 查看次数: |
8484 次 |
| 最近记录: |