/product?id=1&name=2 => ActivateRoute.queryParams[id]
{path:/product/:id} => /product/1 => ActivateRoute.params[id]
{path:product, component:ProductComponent, data:[{isProd:true}] => ActivatedRoute.data[0][isProd]
事件 | description |
---|---|
NavigationStart | 事件開始時觸發 |
RoutesRecognized | 在解析完URL,並識別出了相應的路由時觸發 |
RouteConfigLoadStart | 在Router對一個路由配置進行惰性加載以前觸發 |
RouteConfigLoadEnd | 在Router被惰性加以後觸發 |
NavigationEnd | 導航成功以後觸發 |
NavigationCancel | 導航被取消以後觸發。多是由於導航期間某個路由守衛返回了false |
NavigationError | 在導航發生意外的錯誤時觸發 |
語法:數組
const routes: Router = [ { path: 'home', component: HomeComponent }, { path: 'others', component: OthersComponent, children: [ path: '', component: XxxComponent, path: 'yyy', component: YyyComponent ] }, ]
<router-outlet name="aux"></router-outlet>
具體設置:{ path: 'consult', component: ConsultComponent, outlet: 'aux'}
dom
在設置路由守衛時需先作下面兩步:ide
1、在module
中添加providers
2、在routing.module
中添加須要守衛的路由的canActivate
、canDeactivate
或者Resolve
,前兩個是數組形式,Resolve
是對象形式。
CanActivate:處理導航到某路由的狀況
在guard文件中實現CanActivate
接口:this
canActivate() { var hasPermission:boolean = Math.random() < 0.5; if(!hasPermission) { console.log("用戶無權訪問次股票詳情") } return hasPermission; }
CanDeactivate:處理從當前路由離開的狀況
在guard文件中實現CanDeActivate
接口:code
canDeactivate(component: StockComponent){ if(component.isFocus()){ return true; }else{ return window.confirm("關注一下哦。!") } }
Resolve:在路由激活以前獲取路由數據
在guard文件中實現Resolve
接口component
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any { let id = route.params["id"]; if(id == 1){ return new Stock(1, "IBM"); }else { this.router.navigate(['/home']); return undefined; } }