溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Angular4中路由Router類如何實現跳轉navigate功能

發布時間:2021-07-19 10:44:31 來源:億速云 閱讀:223 作者:小新 欄目:web開發

這篇文章主要為大家展示了“Angular4中路由Router類如何實現跳轉navigate功能”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Angular4中路由Router類如何實現跳轉navigate功能”這篇文章吧。

首先路由配置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 {}

 其次路由跳轉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.以根路由跳轉/login

this.router.navigate(['login']);

2.設置relativeTo相對當前路由跳轉,route是ActivatedRoute的實例,使用需要導入ActivatedRoute

this.router.navigate(['login', 1],{relativeTo: route});

3.路由中傳參數 /login?name=1

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

4.preserveQueryParams默認值為false,設為true,保留之前路由中的查詢參數/login?name=1 to /home?name=1

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

5.路由中錨點跳轉 /home#top

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

6.preserveFragment默認為false,設為true,保留之前路由中的錨點/home#top to /role#top

this.router.navigate(['/role'], { preserveFragment: true });

7.skipLocationChange默認為false,設為true,路由跳轉時瀏覽器中的url會保持不變,但是傳入的參數依然有效

this.router.navigate(['/home'], { skipLocationChange: true });

8.replaceUrl默認為true,設為false,路由不會進行跳轉

this.router.navigate(['/home'], { replaceUrl: true });

以上是“Angular4中路由Router類如何實現跳轉navigate功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女