Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 error: Cannot resolve all parameters for 'RouteParams'

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)...

like image 590
EricC Avatar asked Apr 08 '16 09:04

EricC


1 Answers

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

like image 200
Günter Zöchbauer Avatar answered Sep 18 '22 19:09

Günter Zöchbauer