Mark Nugent
Mark Nugent

Reputation: 609

How to use ActivatedRoute in Angular 5?

I am trying to do exactly the same thing as in this post: Angular 4 get queryString

I am using Angular 5.2.5.

ActivatedRoute seems to be the thing to use to retrieve querystring values off the URL when the user first visits the website. However, I am unable to figure out what I need to import to be able to use ActivatedRoute.

Could someone specify exactly what needs to be added to the app.module.ts file, and the component.ts file where I am trying to use ActivatedRoute?

This post specifies adding routing to the imports array of the @NgModule: No provider for ActivatedRoute - Angular 2 RC5. However, I don't have an app.routing.ts file. Do I have to create an app.routing.ts file to use ActivatedRoute?

Upvotes: 15

Views: 63830

Answers (5)

Prasenjit Mahato
Prasenjit Mahato

Reputation: 1324

How to Get Route Parameters: The Angular Router provides two different methods to get route parameters:

a. Using the route snapshot(ActivatedRoute),

b. Using Router Observables

ActivatedRoute in Angular: Provides access to information about a route associated with a component that is loaded in an outlet

Step-1 Import the ActivatedRoute interface

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

Step-2 Inject the ActivatedRoute in Constructor

constructor(private route: ActivatedRoute, private router: Router) {}

Step-3 To fetch a employee object by the given id and assign that object to its local employee property.

ngOnInit() {
    this.employee = new Employee();
this.id = this.route.snapshot.params['id']; 

Note: Property Description

  snapshot: The current snapshot of this route

Upvotes: 2

Mickey Mouse
Mickey Mouse

Reputation: 35

ActivatedRoute

I'm late to the conversation but hope the following works for the future programmers who encounter the same issue.

import the ActivatedRoute

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

Inject the dependency injection

constructor(
    private route: ActivatedRoute,
  ) { }

and to grab the id from the link you can use the following

  ngOnInit() {
  this.route.paramMap.subscribe(params => {
    this.product = products[+params.get('productId')];
  });
}

Upvotes: 0

Mr. Droid
Mr. Droid

Reputation: 340

ActivatedRoute Contains the information about a route associated with a component loaded in an outlet. It can also be used to pass data from one component to another component using route such as Id, flag, state etc.

http://localhost:4200/quiz/edit_quiz/032

032 being id of the quiz you wanna edit.

Get this id in your component(in my case let it be edit_quiz.compontent.ts) to use by using Activated Route.

Step 1: Import ActivatedRoute from Router module.

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

Step 2: Inject ActivatedRoute in constructor.

  constructor(private activatedRoute: ActivatedRoute) { }

Step 3: Get id on a local variable named quizId in ngOnInit(){}

   ngOnInit() {
    this.quiz_id = this.activatedRoute.snapshot.params['id'];
   }

Now we have id in edit_quiz.component.ts to use.

Upvotes: 19

Arun Amatya
Arun Amatya

Reputation: 51

You need to import ActivatedRoute from @angular/router like

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

then add this line to the imports array of the @NgModule in app.module.ts:

imports:[
        ........,
        RouterModule.forRoot()
],

then you can use any where as below:

constructor(private route: ActivatedRoute) {
     console.log(route.snapshot.queryParamMap); // this
}
// or
queryString : string;
getQueryString(){
   this.queryString = this.route.queryParamMap.get('myQueryParam');
}

No. You don't need app.routing.ts if you don't have to navigate pages within your app.

Upvotes: 2

Mark Nugent
Mark Nugent

Reputation: 609

I made the two changes Arun suggested. Then, to fix the "No provider for ActivatedRoute" error, I made the changes shown below.

1) I added this line to the app.module.ts:

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

2) I added this line to the imports array of the @NgModule in app.module.ts:

RouterModule.forRoot([])

This article gave me the fix: Angular error: no provider for ActivatedRoute

Now it compiles. Hooray!

Upvotes: 3

Related Questions