詳解Angular4中路由Router類(lèi)的跳轉(zhuǎn)navigate
最近一直在學(xué)習(xí)angular4,它確實(shí)比以前有了很大的變化和改進(jìn),好多地方也不是那么容易就能理解,好在官方的文檔和例子是中文,對(duì)英文不太好的還是有很大幫助去學(xué)習(xí)。
官方地址:https://angular.cn/docs/ts/latest/api/router/index/Router-class.html
在學(xué)習(xí)的過(guò)程中路由(router)機(jī)制是離不開(kāi)的,并且好多地方都要用到。
首先路由配置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相對(duì)當(dāng)前路由跳轉(zhuǎn),route是ActivatedRoute的實(shí)例,使用需要導(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,保留之前路由中的查詢(xún)參數(shù)/login?name=1 to /home?name=1
this.router.navigate(['home'], { preserveQueryParams: true });
5.路由中錨點(diǎn)跳轉(zhuǎn) /home#top
this.router.navigate(['home'],{ fragment: 'top' });
6.preserveFragment默認(rèn)為false,設(shè)為true,保留之前路由中的錨點(diǎn)/home#top to /role#top
this.router.navigate(['/role'], { preserveFragment: true });
7.skipLocationChange默認(rèn)為false,設(shè)為true,路由跳轉(zhuǎn)時(shí)瀏覽器中的url會(huì)保持不變,但是傳入的參數(shù)依然有效
this.router.navigate(['/home'], { skipLocationChange: true });
8.replaceUrl默認(rèn)為true,設(shè)為false,路由不會(huì)進(jìn)行跳轉(zhuǎn)
this.router.navigate(['/home'], { replaceUrl: true });
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ionic使用angularjs表單驗(yàn)證(模板驗(yàn)證)
能夠驗(yàn)證用戶(hù)在表單中輸入的內(nèi)容是否合理與正確是十分重要的,這篇文章主要介紹了ionic使用angularjs表單驗(yàn)證(模板驗(yàn)證),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12AngularJs的$http發(fā)送POST請(qǐng)求,php無(wú)法接收Post的數(shù)據(jù)問(wèn)題及解決方案
這篇文章主要介紹了AngularJs的$http發(fā)送POST請(qǐng)求,php無(wú)法接收Post的數(shù)據(jù)的問(wèn)題及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例
這篇文章主要介紹了Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01

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

淺談Angular2 ng-content 指令在組件中嵌入內(nèi)容