1

I am trying to pass and read data using Angular Route.navigate ,

On one component I have

this._router.navigate(["dashboard",{ queryParams: { page: 1 }}],

On Dashboard component I have

ngOnInit() {
    this.sub = this._route
      .queryParams
      .subscribe(params => {
        // Defaults to 0 if no query param provided.
        let page = +params['page'] || 0;
        console.log(page);
      });
  }

But this always return 0

Am I doing anything wrong?

My ng --version is

@angular/cli: 1.0.2
node: 6.3.1
os: darwin x64
@angular/common: 4.1.1
@angular/compiler: 4.1.1
@angular/core: 4.1.1
@angular/forms: 4.1.1
@angular/http: 4.1.1
@angular/platform-browser: 4.1.1
@angular/platform-browser-dynamic: 4.1.1
@angular/router: 4.1.1
@angular/cli: 1.0.2
@angular/compiler-cli: 4.1.1
IPLCEWKS01167:routeTest iplcewks01167$

4 Answers 4

2

queryParams should be the second parameter for navigate.

replace this line

this._router.navigate(["dashboard",{ queryParams: { page: 1 }}];

to

this._router.navigate(["dashboard"], { queryParams: { page: 1 }});
Sign up to request clarification or add additional context in comments.

3 Comments

I dont know the reason , but this is not working. But this works :: this._router.navigate(["dashboard",{ page: 1 }] :: this.sub = this._route.params .subscribe(params => { console.log(params); // Defaults to 0 if no query param provided. let page = +params['page'] || 0; console.log(page); });
@raju yes, they are two different way. when using queryParams, it should be setted to the second parameter.
@raju and the previous one is syntax for params and the second is for queryParams.
1

Try this :-

this.router.navigate(['/dashboard', { queryParams: { page: 1 }} ]);

Comments

0

Try this

{ path: 'a4/:message', component:  Angular4Component, data:{ ping:'passedvia router'}}

fetch it in the component using this.message = this.route.snapshot.params['message'];

this is the file in which it is used in the repo .

https://github.com/rahulrsingh09/AngularConcepts/blob/master/src/app/angular4/angular4.component.ts

The working example for the same - https://rahulrsingh09.github.io/AngularConcepts

Comments

0

Angular 7.2.0 introduced new way of passing the data when navigating between routed components without the data being a part of the query string.

Sending the data

export class ComponentA {
    constructor(private router: Router) {}
    goToComponentB(): void {
        this.router.navigate(['routePath'], {state: {data: {...}}});
    }
}

Receiving the data

You can read the data from browser’s history object in the constructor:

constructor(){
   Console.log(history.state.data);
}

Reference - https://medium.com/ableneo/how-to-pass-data-between-routed-components-in-angular-2306308d8255

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.