Trying to use RouteParams to get query-string parameters, but I just get the error
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:332 Error: Cannot read property 'getOptional' of undefined.......
Check out this Plnkr. How can I use RouteParams without getting this warning?
The important files are:
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]);
and 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');
}
}
Update:
I am not having any routes, I just have the default root route (if that can be called a route at all when I have no other routes...). But as suggested by Gianluca, I added the following route config:
@RouteConfig([
{ path: '/:guid', name: 'Home', component: AppComponent, useAsDefault: true },
])
But I still get the same error (the Plnkr is updated)...
Remove RouteParams
from
bootstrap(AppComponent, [ROUTER_PROVIDERS, RouteParams]);
RouteParams
is provided by the router. If you provide it yourself injecting it fails.
See https://angular.io/api/router/Params for an example. RouteParams
can only be injected on components added by the router.
Plunker example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With