Table of Contents
navigateByUrl
navigate
Home Web Front-end JS Tutorial A brief discussion on navigateByUrl and navigate in Angular routing jumps

A brief discussion on navigateByUrl and navigate in Angular routing jumps

Jul 22, 2021 am 10:53 AM
angular

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  // 相对路径。从当前路径开始
Copy after login

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

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

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

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

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

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

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

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

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

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

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 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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let's talk about metadata and decorators in Angular Let's talk about metadata and decorators in Angular Feb 28, 2022 am 11:10 AM

This article continues the learning of Angular, takes you to understand the metadata and decorators in Angular, and briefly understands their usage. I hope it will be helpful to everyone!

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

Angular + NG-ZORRO quickly develop a backend system Angular + NG-ZORRO quickly develop a backend system Apr 21, 2022 am 10:45 AM

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

See all articles