Home > Web Front-end > JS Tutorial > 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

青灯夜游
Release: 2021-11-12 11:07:29
forward
3044 people have browsed it

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){} 
}
Copy after login

[Related tutorial recommendation: "angular tutorial" 】

1. Differences

1.1 navigateByUrl()

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

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

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

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>
Copy after login

The first parameter is an array

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

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})
Copy after login

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?: {...}
}
Copy after login

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})
Copy after login

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}
    });
}
Copy after login
  • 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}
    });
}
Copy after login

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);
  }
}
Copy after login

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 就是传过来的数据
  }
}
Copy after login

2.2 Both have callbacks

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

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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template