Home  >  Article  >  Web Front-end  >  Jump navigation of Router class in Angular4

Jump navigation of Router class in Angular4

不言
不言Original
2018-05-05 15:57:502713browse

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([&#39;login&#39;]);

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([&#39;login&#39;, 1],{relativeTo: route});

3. Pass the parameter /login?name=1## in the route.

#

this.router.navigate([&#39;login&#39;, 1],{ queryParams: { name: 1 } });

4.preserveQueryParams default value is false, set to true, retain the query parameters /login?name=1 to /home?name=1# in the previous route

##
this.router.navigate([&#39;home&#39;], { preserveQueryParams: true });

5. Anchor jump in routing /home#top

 this.router.navigate([&#39;home&#39;],{ fragment: &#39;top&#39; });

6 .preserveFragment defaults to false, set to true, and retains the anchor point /home#top to /role#top

this.router.navigate([&#39;/role&#39;], { 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([&#39;/home&#39;], { skipLocationChange: true });

8.replaceUrl default is true, set to false, the route will not jump

this.router.navigate([&#39;/home&#39;], { 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!

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