


A 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().
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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!

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

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!

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!

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

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!

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

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!
