Home  >  Article  >  Web Front-end  >  A brief analysis of the differences and commonalities between navigateByUrl and navigate in Angular routing

A brief analysis of the differences and commonalities between navigateByUrl and navigate in Angular routing

青灯夜游
青灯夜游forward
2021-11-12 11:07:292992browse

This article will compare navigateByUrl and navigate in Angular routing to see their differences and commonalities. I hope it will be helpful to everyone!

A brief analysis of the differences and commonalities between navigateByUrl and navigate in Angular routing

angular navigateByUrl vs navigate route jump

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

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

[Related tutorial recommendation: "angular tutorial" 】

1. Differences

1.1 navigateByUrl()

navigateByUrl(url: string | UrlTree, extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>

The first parameter must be a string of == absolute path==.

 this.router.navigateByUrl(&#39;/home&#39;);

The first parameter they receive is different, and the second parameter is the same.

1.2 navigate()

navigate(commands: any[], extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>

The first parameter is an array

this.router.navigate([&#39;home&#39;, &#39;demo&#39;])

Then the parsed route islocalhost:4200/home/demo.

You can navigate relative to the current route

Pass in a relativeTo parameter to jump relative to the incoming route. For example, currently in localhost:4200/home, the address after

this.router.navigate([&#39;demo&#39;], {relativeTo: this.route})

jumps is localhost:4200/home/demo.

But if 'demo' is written as '/demo', the incoming route will not work, and the root route will be used for navigation. If not passed in, the root route (localhost:4200) will be used for navigation by default.

2. Common points:

interface NavigationExtras {
  relativeTo?: ActivatedRoute | null
  queryParams?: Params | null
  fragment?: string
  preserveQueryParams?: boolean
  queryParamsHandling?: QueryParamsHandling | null
  preserveFragment?: boolean
  skipLocationChange?: boolean
  replaceUrl?: boolean
  state?: {...}
}

2.1 The method of passing parameters is the same

Take navigation as an example

Passing parameters through queryParams

This method of passing parameters will splice the parameters on the url, such as localhost:4200/demo?id=1

A component Pass parameters

this.router.navigate([&#39;demo&#39;], {queryParams: {id: 1} , relativeTo: this.route})

B component receives parameters

  • If passed through /user/:id, use activatedRoute.params
import { ActivatedRoute } from &#39;@angular/router&#39;;

constructor(private activatedRoute: ActivatedRoute) {
    this.activatedRoute.params.subscribe((param) => {
      console.log(&#39;组件里面的param&#39;, param);// {id :1}
    });
}
  • If passed through /user?id=1, use activatedRoute.queryParams
import { ActivatedRoute } from &#39;@angular/router&#39;;

constructor(private activatedRoute: ActivatedRoute) {
    this.activatedRoute.queryParams.subscribe((param) => {
      console.log(&#39;组件里面的queryParams&#39;, param); // {id :1}
    });
}

Passing parameters through state

This method will store the data in the browser's history. The state must be an object and be retrieved using getCurrentNavigation in the sub-route.

A component passes parameters

import { Component, Input } from &#39;@angular/core&#39;;
import { Router, NavigationExtras } from &#39;@angular/router&#39;;

@Component({
  selector: &#39;a-component&#39;,
  template: `
    <button (click)="test()">Test</button>
  `,
})
export class AComponent  {
  constructor(private router: Router){}

  test(){
    const navigationExtras: NavigationExtras = {state: {id: 1}};
    this.router.navigate([&#39;b&#39;], navigationExtras);
  }
}

B component receives parameters

import { Component } from &#39;@angular/core&#39;;
import { Router } from &#39;@angular/router&#39;;

@Component({
  selector: &#39;b-component&#39;
})
export class BComponent {
  constructor(private router: Router) { 
    const navigation = this.router.getCurrentNavigation();
    const state = navigation.extras.state as {id: number};
    // state.id 就是传过来的数据
  }
}

2.2 Both have callbacks

 this.router.navigate([&#39;demo&#39;]).then(nav => {
    console.log(nav); // true: 跳转成功, false 跳转失败
  }, err => {
    console.log(err) // 发生无措
  });

More usage updates on github:

https://github.com/deepthan/blog-angular

For more programming-related knowledge, please visit:ProgrammingIntroduction! !

The above is the detailed content of A brief analysis of the differences and commonalities between navigateByUrl and navigate in Angular routing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete