search
HomeWeb Front-endJS TutorialA brief discussion on navigateByUrl and navigate in Angular routing jumps

This article will introduce to you Angular NavigateByUrl and navigate in Router routing jump, and see how to use navigate() and navigateByUrl().

A brief discussion on navigateByUrl and navigate in Angular routing jumps

Before we start the actual combat, let’s take a look at the introduction of navigateByUrl and navigate in the official documents. [Related tutorial recommendations: "angular tutorial"]

navigateByUrl():

Definition: Navigate based on the provided URL, Absolute path must be used
Parameters: url (string | UrlReee), extras (an object containing a set of properties, which will modify the navigation strategy)
Return value: Returns a Promise. When the navigation is successful, it will resolve to true; when the navigation fails or an error occurs, it will resolve to false

ps: The official explanation of the usage and definition of navigateByUrl has been very clear. However, if our memory of the definition of absolute path and relative path is a bit vague, then I will give an example directly so that I won’t bother the little ones to go find Du Niang again. Who made me considerate

E:\mySoft\Git\bin  // 绝对路径。从盘符开始
Git\bin  // 相对路径。从当前路径开始

navigate():

Definition: Navigate based on the provided command array and origin route. If the starting point route is not specified, absolute navigation starts from the root route
Parameters: commands (any[]), extras
Return value: Returns a Promise. When the navigation is successful, it will resolve to true; when the navigation fails, it will resolve to false; when the navigation error occurs, it will reject (reject)

The noteworthy point is that the first of navigate Each parameter must be in the form of an array, that is, any[].

Getting back to the topic, returning to the function, these two methods are used for routing jumps in angular. Then we have the following common xxx usages in actual projects. Let’s take a look at them one by one~~


In actual combat, we first define three routes, namely "routing" a, route b, route c”.
These three routes are sibling routes and are all in the root directory.


navigateByUrl

路由a跳转到路由b
this.router.navigateByUrl('b');  // 正确。解析结果是 localhost:4200/b
this.router.navigateByUrl('./b');  // 错误。只能是绝对路径哦

路由b跳转到路由c
this.router.navigateByUrl('cascader', {});  // 解析结果是 localhost:4200/c

The usage of navigateByUrl is relatively simple, easy to understand, and the usage is relatively simple. We mainly introduce the following usage of navigate~~

navigate

1. Route b jumps to route c (jump based on the root route)

this.router.navigate(['c']);  // 绝对路径。 localhost:4200/c
this.router.navigate(['./c']);  // 相对路径。 localhost:4200/c

2. Route b jumps to route c (jump based on the current route)

this.router.navigate(['c'],{ relativeTo:this.route });  // localhost:4200/b/c
this.router.navigate(['c',1],{ relativeTo:this.route });  // localhost:4200/b/c/1

3. Route b jumps to route b (jump based on the current route) )

this.router.navigate([],{ relativeTo:this.route });  // localhost:4200/b

4. Route b jumps to route c (the anchor point is carried in the route to jump)

this.router.navigate(['c'],{ fragment:'zita' });  // localhost:4200/c#zita
	现在么,成功跳转到路由c了。我又想从路由c跳转到路由a(携带锚点跳转)
	this.router.navigate(['a'], { preserveFragment: true});  // localhost:4200/a#zita

5. Route b jumps to route c (the parameter is passed in the route to jump) Transfer)

this.router.navigate(['c'],{ queryParams:{name:'zita'} });  // localhost:4200/c?name=zita
	现在么,成功跳转到路由c了。我又想从路由c跳转到路由a,有以下五种情况:
	
	(1)不携带参数跳转
	this.router.navigate(['a'], { queryParamsHandling: null });  // localhost:4200/a
	(2)携带参数跳转
	this.router.navigate(['a'], { queryParamsHandling: 'merge'});  // localhost:4200/a?name=zita
	
	执行完以下三种情况的代码后,看到的页面是路由a的页面哦!
	(3)携带参数。浏览器中的URL不变,参数会失效即,在路由a中打印的参数结果是{}
	this.router.navigate(['a'], { skipLocationChange: true });  // localhost:4200/c?name=zita
	(4)携带参数。浏览器中的URL不变,参数有效。在路由a中打印的参数结果是{name: "zita"}
	this.router.navigate(['a'], {skipLocationChange: true, queryParamsHandling: 'merge'});  // localhost:4200/c?name=zita
	(5)携带参数。浏览器中的URL不变,参数有效,并且携带上其他参数。在路由a中打印的参数结果是{name: "zita",sex: "female"}
	this.router.navigate( ['a'], {skipLocationChange: true, queryParamsHandling: 'merge', queryParams: { sex: 'female' } });  // localhost:4200/c?name=zita

6. Route b jumps to route c (the current status will not be recorded in the history during navigation)

 在路由c中,点击浏览器的返回按钮,会忽略路由b而直接跳转回到路由b的上一层路由
this.router.navigate(['c'],{ replaceUrl:true });  // localhost:4200/c

A brief discussion on navigateByUrl and navigate in Angular routing jumps

Finally, Little cuties~

Don’t forget to introduce router when using routing~~

import { Router } from '@angular/router';
constructor( private router: Router) { }

In addition, if you want to print the parameters carried, the code snippet is as follows:

import { Router, ActivatedRoute, Params } from '@angular/router';
ngOnInit() {
    this.route.queryParams.subscribe((params: Params) => {
      console.log(params);
    });
}

happyEnding…

For more programming-related knowledge, please visit: Introduction to Programming! !

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

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
聊聊Angular中的元数据(Metadata)和装饰器(Decorator)聊聊Angular中的元数据(Metadata)和装饰器(Decorator)Feb 28, 2022 am 11:10 AM

本篇文章继续Angular的学习,带大家了解一下Angular中的元数据和装饰器,简单了解一下他们的用法,希望对大家有所帮助!

angular学习之详解状态管理器NgRxangular学习之详解状态管理器NgRxMay 25, 2022 am 11:01 AM

本篇文章带大家深入了解一下angular的状态管理器NgRx,介绍一下NgRx的使用方法,希望对大家有所帮助!

浅析angular中怎么使用monaco-editor浅析angular中怎么使用monaco-editorOct 17, 2022 pm 08:04 PM

angular中怎么使用monaco-editor?下面本篇文章记录下最近的一次业务中用到的 monaco-editor 在 angular 中的使用,希望对大家有所帮助!

项目过大怎么办?如何合理拆分Angular项目?项目过大怎么办?如何合理拆分Angular项目?Jul 26, 2022 pm 07:18 PM

Angular项目过大,怎么合理拆分它?下面本篇文章给大家介绍一下合理拆分Angular项目的方法,希望对大家有所帮助!

聊聊自定义angular-datetime-picker格式的方法聊聊自定义angular-datetime-picker格式的方法Sep 08, 2022 pm 08:29 PM

怎么自定义angular-datetime-picker格式?下面本篇文章聊聊自定义格式的方法,希望对大家有所帮助!

Angular + NG-ZORRO快速开发一个后台系统Angular + NG-ZORRO快速开发一个后台系统Apr 21, 2022 am 10:45 AM

本篇文章给大家分享一个Angular实战,了解一下angualr 结合 ng-zorro 如何快速开发一个后台系统,希望对大家有所帮助!

聊聊Angular Route中怎么提前获取数据聊聊Angular Route中怎么提前获取数据Jul 13, 2022 pm 08:00 PM

Angular Route中怎么提前获取数据?下面本篇文章给大家介绍一下从 Angular Route 中提前获取数据的方法,希望对大家有所帮助!

浅析Angular中的独立组件,看看怎么使用浅析Angular中的独立组件,看看怎么使用Jun 23, 2022 pm 03:49 PM

本篇文章带大家了解一下Angular中的独立组件,看看怎么在Angular中创建一个独立组件,怎么在独立组件中导入已有的模块,希望对大家有所帮助!

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!