国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解Angular4中路由Router類的跳轉(zhuǎn)navigate

2024-05-06 15:16:05
字體:
供稿:網(wǎng)友

最近一直在學(xué)習(xí)angular4,它確實比以前有了很大的變化和改進(jìn),好多地方也不是那么容易就能理解,好在官方的文檔和例子是中文,對英文不太好的還是有很大幫助去學(xué)習(xí)。

官方地址:https://angular.cn/docs/ts/latest/api/router/index/Router-class.html

在學(xué)習(xí)的過程中路由(router)機制是離不開的,并且好多地方都要用到。

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

 其次路由跳轉(zhuǎn)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.以根路由跳轉(zhuǎn)/login

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

2.設(shè)置relativeTo相對當(dāng)前路由跳轉(zhuǎn),route是ActivatedRoute的實例,使用需要導(dǎo)入ActivatedRoute

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

3.路由中傳參數(shù) /login?name=1

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

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

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

5.路由中錨點跳轉(zhuǎn) /home#top

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

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

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

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

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

8.replaceUrl默認(rèn)為true,設(shè)為false,路由不會進(jìn)行跳轉(zhuǎn)

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 新郑市| 昆山市| 翁牛特旗| 桃源县| 柳河县| 丽江市| 澄江县| 息烽县| 长阳| 金华市| 普陀区| 卢龙县| 中超| 通海县| 红安县| 浦江县| 理塘县| 扬中市| 突泉县| 邵阳县| 潍坊市| 云龙县| 大宁县| 上杭县| 茶陵县| 尉犁县| 稷山县| 宝清县| 浮梁县| 额济纳旗| 塔河县| 丰宁| 皋兰县| 晋宁县| 曲水县| 邢台市| 漳浦县| 茂名市| 东源县| 尉氏县| 杨浦区|