-1

I've tried the sample on the official page but neither it works nor the other examples on other pages work me. There are no params neither of the router nor the route export. How can I subscribe and get the parameter change? Neither can I use the RouteParams, I got an exception there is no provider for it. Thank you!

How am I able to do this with the current version of angular 2?

private sub: any;

    ngOnInit() {
      this.sub = this.route.params.subscribe(params => {
         let id = +params['id']; // (+) converts string 'id' to a number
         this.service.getHero(id).then(hero => this.hero = hero);
       });
    }

my code:

@RouteConfig([
    { path: '/:appleId/:bananaId/fruits/...', name: 'Fruits', component: FruitsComponent }
])

How Can I get the appleId or bananaId change in the FruitsComponent? The this.route.params does not exist :( What should I use instead?

Lajos
  • 2,549
  • 6
  • 31
  • 38
  • 2
    This only works on the component the routes contain parameters. For example if the parameter is for a child route this only works on the component of that child route but not in `AppComponent`. See also http://stackoverflow.com/questions/38460470/accessing-route-data-in-root-component-route-data-is-empty – Günter Zöchbauer Jul 19 '16 at 15:28
  • 1
    Please add the routes and what component it is that contains the code of your question. – Günter Zöchbauer Jul 19 '16 at 15:29
  • at fist thank you, for your answer, I updated the code. Could you have a look at it, please? – Lajos Jul 19 '16 at 15:48
  • 1
    So you are using router-deprecated? The `ngOnInit()` code looks like for the new router but the `@RouteConfig` is for the deprecated beta router. – Günter Zöchbauer Jul 19 '16 at 15:49
  • I am using import { Component, OnInit } from 'angular2/core'; import {ROUTER_DIRECTIVES, RouteConfig, Router, RouteParams} from 'angular2/router'; – Lajos Jul 19 '16 at 15:50
  • 1
    That means you are using a beta version. Are there any reasons you're not updating to the latest (RC.4)? – Günter Zöchbauer Jul 19 '16 at 15:52
  • :O I thought I am up to date "angular2": "2.0.0-beta.17" I need to check it what is going here. Thank you so far! – Lajos Jul 19 '16 at 15:55
  • 1
    New Angular2 is under `@angular` instead of `angular2` – Günter Zöchbauer Jul 19 '16 at 15:57
  • 1
    You were right, I updated and it works. Thank you! – Lajos Jul 19 '16 at 18:36

2 Answers2

0

Try below one:

@RouteConfig([
    { 
        path: '/:id', name: 'Fruits', component: FruitsComponent 
    }
])
ngOnInit() {
    this.sub = this.route.snapshot.params['id'];
    if(this.sub == 'appleId/:bananaId/fruits'){
        // your code
    }
}
0

this is the most suitable to get parameter from url

import {ActivatedRoute} from '@angular/router';

constructor(private activatedRoute: ActivatedRoute) {}

this.activatedRoute.queryParams.subscribe(params => {
  let id = = params['id'];
});
Ragulan
  • 1,677
  • 17
  • 24