0

I am trying to use the route params but not able to achieve it since when i importing the Routeparams it shows errors,,

My template,

    <h6 class="headingh6 nobottommargin"><a [routerLink]="['User',{{detail.firstname}}]">  {{detail.firstname}} </a></h6>

My componenet

import { Component,OnInit} from '@angular/core';
import { Route, RouteSegment, ROUTER_DIRECTIVES } from '@angular/router';

@Component({
selector: 'User', 
templateUrl: './components/society/society.html',
Directives: [ROUTER_DIRECTIVES]
})
export class User  {
id:any;
 constructor(routeSegment: RouteSegment) {
   this.id = routeSegment.getParam('id'); 
   console.log(this.id);   
 }
}

Can someone help me

My routes

   import {provideRouter, RouterConfig} from '@angular/router';

  import {DemoPage} from './demo-page';

  import {User} from './components/user/user';


   export const routes: RouterConfig = [
   { path: '', redirectTo: '/login', terminal: true },
   { path: 'login', component:Login },
   { path: 'signup', component:SignUp },
   { path: 'demo', component: DemoPage, children: [
     { path: 'user', component:User },
     { path: 'requests', component:Requests },
   ]}
];

 export const APP_ROUTER_PROVIDERS = [
    provideRouter(routes)


  ];

2 Answers 2

2
<h6 class="headingh6 nobottommargin"><a [routerLink]="['User',detail.firstname]">{{detail.firstname}}</a></h6>

Don't use [] together with {{}}. Either one or the other, but not both at the same time.

To get router parameters use

constructor(private route: ActivatedRoute) {
  route.routerState.params.subscribe(p => console.log(p['id'));
}

See also Get route query params

Sign up to request clarification or add additional context in comments.

17 Comments

What router version are you using? There is no RouteSegment in the current router version.
can have any alternative
Three are 3 (if you count ngrx router then 4) different routers for Angular2 and each one in different versions. I updated my answer for the V3.0.0-beta.2 (the most current from the Angular2 team) router. The other two routers from the Angular2 team are deprecated.
When i runned it says error that "Can't bind to 'routerLink' since it isn't a known native property "
You need to add @Component({ ..., directives: [ROUTER_DIRECTIVES]})` on components where you want to use <router-outlet> or routerLink.
|
2

Do you just want to read the Querystring param ?

if yes, use this

constructor(private route: ActivatedRoute) {
      console.log(this.route.snapshot.params.id);
   }

in the import you need this:

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

of course this will work if in the route config you have /:id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.