/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; } }