這篇文章主要介紹了詳解Angular4中路由Router類的跳轉navigate,具有一定的參考價值,感興趣的小伙伴們可以參考一下
最近一直在學習angular4,它確實比以前有了很大的變化和改進,好多地方也不是那麼容易就能理解,好在官方的文檔和例子是中文,對英文不太好的還是有很大幫助去學習。
在學習的過程中路由(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 {}
# 其次路由跳轉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});
this.router.navigate(['login', 1],{ queryParams: { name: 1 } });
this.router.navigate(['home'], { preserveQueryParams: true });
this.router.navigate(['home'],{ fragment: 'top' });
this.router.navigate(['/role'], { preserveFragment: true });
#
this.router.navigate(['/home'], { skipLocationChange: true });
this.router.navigate(['/home'], { replaceUrl: true });
以上是Angular4中路由Router類別的跳轉navigate的詳細內容。更多資訊請關注PHP中文網其他相關文章!