Jump navigation of Router class in Angular4
This article mainly introduces the detailed explanation of the jump navigation of the Router class in Angular4. It has certain reference value. Interested friends can refer to it.
I have been learning angular4 recently. It is indeed better than There have been great changes and improvements in the past, but many places are not so easy to understand. Fortunately, the official documents and examples are in Chinese, which is still very helpful for those who are not good at English.
In the process of learning, the routing (router) mechanism is inseparable and is used in many places.
First route configuration Route:
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home.component'; import { LoginComponent } from './login.component'; import { RegisterComponent } from './register.component'; const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'login', component: LoginComponent }, { path: 'heroes', component: RegisterComponent } ]; @NgModule({ imports: [ RouterModule.forRoot(routes) ], exports: [ RouterModule ] }) export class AppRoutingModule {}
Secondly route jump Router.navigate
navigate(commands: any[], extras?: NavigationExtras) : Promise<boolean>
interface NavigationExtras { relativeTo : ActivatedRoute queryParams : Params fragment : string preserveQueryParams : boolean queryParamsHandling : QueryParamsHandling preserveFragment : boolean skipLocationChange : boolean replaceUrl : boolean }
1. Jump to /login with the root route
this.router.navigate(['login']);
2.Settings relativeTo jumps relative to the current route. route is an instance of ActivatedRoute. To use it, you need to import ActivatedRoute
this.router.navigate(['login', 1],{relativeTo: route});
3. Pass the parameter /login?name=1## in the route.
#
this.router.navigate(['login', 1],{ queryParams: { name: 1 } });
this.router.navigate(['home'], { preserveQueryParams: true });
5. Anchor jump in routing /home#top
this.router.navigate(['home'],{ fragment: 'top' });
6 .preserveFragment defaults to false, set to true, and retains the anchor point /home#top to /role#top
this.router.navigate(['/role'], { preserveFragment: true });
7.skipLocationChange defaults to false, set to true, the url in the browser will remain unchanged when the route jumps, but the parameters passed in are still valid
this.router.navigate(['/home'], { skipLocationChange: true });
8.replaceUrl default is true, set to false, the route will not jump
this.router.navigate(['/home'], { replaceUrl: true });
The above is the detailed content of Jump navigation of Router class in Angular4. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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





ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

VueRouter is a routing management plug-in officially provided by Vue.js. It can help us implement page navigation and route switching in Vue applications. The Lazy-Loading feature is a unique advantage of VueRouter, which can greatly optimize page performance. In this article, we will introduce VueRouter’s Lazy-Loading routing feature and provide some practical code examples for optimizing page performance. Lazy-Loading means when needed

Solution to react router not displaying: 1. Add browserRouter to the parent routing component to wrap the router; 2. Use "this.props.history.go()" to refresh the component; 3. Add "forcerefresh" to the browserrouter parameter ={true}"; 4. Write a hook function in "<Route>" and call it when leaving or entering this route.

VueRouter is the official route manager in the Vue.js framework. It allows developers to switch page content through route mapping, making single-page applications more controllable and easier to maintain. However, as applications become more complex, the loading and parsing of routes can become a performance bottleneck. In order to solve this problem, VueRouter provides a function of lazy loading of routes, which defers the loading of routes until actually needed. Lazy-loading is a loading technology that

1. Route jump 1. First introduce the API—useRouterimport{useRouter}from'vue-router'2. Define the router variable on the jump page // First define constrouter=useRouter() in the setup 3. Use router.push to jump to the page //String router.push('home')//Object router.push({path:'home'})//

How to use Router to implement redirection function in Vue project In a Vue project, we often need to implement jump and redirection functions between pages. And VueRouter provides a simple yet powerful solution. This article will introduce how to use Router to implement the redirection function in the Vue project and give specific code examples. Install VueRouter First, we need to install VueRouter in the Vue project. Can

How is lazy loading of routes implemented in VueRouter? In Vue development, we usually use VueRouter to implement jumps and routing control between pages. When the project becomes large, we may have many routing pages to load, which will cause the entire project to load slowly. In order to improve the performance of the project, VueRouter provides a mechanism for lazy loading of routes. Lazy loading of routes means that the routing page will be loaded only when it is accessed, instead of loading all routes when the application is initialized.

This article will introduce Vue Router route redirection and alias settings through examples. I hope it will be helpful to friends in need!
