【Angular】路由 Route导航
在路由开始之前,先补一下Angular的一些基础知识:
框架认识: Angular Route导航
{ path: '',component:HomeComponent },{ path:'product',component:ProductComponent }
定义根路由:
使用命令导航[routerLink]: 使用代码导航:
<router-outlet></router-outlet >
以上步骤以能成功导航
为了防止程序崩溃,所以设置一个通用路由,即当输入不存的URL时,导航到指定页面 { path:'*',component:Code404Component }
因为路由具有先匹配者优先的原则,所以配置路由时通用路由应该放在所有路由的后面
在URL中传递数据 { path:'product/:id',component:ProductComponent }
2.在跳转路由入口时传递实际的数值 在查询参数中传递数据 ngOnInit() {
//参数快照方式
this.productId = this.routeInfo.snapshot.queryParams["id"];
//参数订阅方式,写在匿名函数中
this.routeInfo.params.subscribe((params:Params) => this.productId=params["id"]);
}
使用参数订阅方式,是因为当有多个方式跳转到同一路由时,但是传递的参数不同,但是ngOnInit()只有在第一次创建对象时才执行,由于我们接收参数时又是写在ngOnInit()里面的,所以如果使用参数快照的方式,只有第一次导航路由时参数被传进来了,以后再导航该路由时,参数都传不进来了,因为不执行ngOnInit()方法了
使用情况: 比如:在设置路由时,默认空路径,导航到HomeComponent,但是这样不符合规范,路径名最好是组件名, { path: 'home',component:HomeComponent }
然后可以用路由重定向,当到默认路径下时,重导向到home路径下 { // 路由重定向 path:'',redirectTo:'/home',pathMatch:'full' }
1.想给谁添加子路由就在谁的路由配置下添加children path:'product',component:ProductComponent,// 定义子路由
children:[
{
path:'',component:ProductDescComponent
}
]
2.在父路由的html中添加一个子路由的插座
1.配置辅助路由出口:一个页面只能有一个路由插座,这是还想一块显示隐藏多个组件,这时就可以定义一个辅助路由 <router-outlet></router-outlet >
<router-outlet name="aux"></router-outlet>
辅助路由与主路由不同的是,要添加一个name属性 2.然后配置路由 { path:'chat',component:ChatComponent,outlet:'aux' }
3.最后定义辅助路由入口
CanActivate:在满足一定条件下才能进入下一个路由 export class LoginGuard implements CanActivate{
canActivate(){
let loggedIn:boolean = Math.random() < 0.5;
if(!loggedIn){
console.log("用户未登录");
}
return loggedIn;
}
}
2.然后在要跳转到的路由配置中,添加 CanDeactivate<返回到的路由组件> :处理从当前路由离开的情况 export class UnsavedGuard implements CanDeactivate<ProductComponent>{
canDeactivate(component: ProductComponent){
return window.confirm("你还没有保存,确定要离开吗?");
}
}
2.然后在要跳转到的路由配置中,添加 Resolve<返回的东西>:在路由激活之前获取路由数据 @Injectable()
export class ProductResolve implements Resolve<Product>{
constructor(private router:Router){ }
resolve(route:ActivatedRouteSnapshot,state:RouterStateSnapshot):Observable<Product>|Promise<Product>|Product{
let productId:number = route.params["id"];
if(productId == 1){
return new Product(1,"iPhone7");//返回Product对象
} else {
this.router.navigate(['/home']);
return undefined;
}
}
注意项: 导入@Injectable()装饰器 构造函数中定义Router对象 返回的是Product对象,所以要在要跳转到的路由的组件中定义Product类 export class Product {
constructor(public id:number,public name:string){}
}
2.然后在要跳转到的路由配置中,添加 export class ProductComponent implements OnInit {
private productId:number;
private productName:string;
constructor(private routeInfo:ActivatedRoute) { }
ngOnInit() {
//订阅传进来的数据
this.routeInfo.data.subscribe((data:{product:Product}) =>{
this.productId = data.product.id;
this.productName = data.product.name;
});
}
}
设置Resolve路由的原因:防止在有插值绑定的html中,在数据没常见来时页面已加载的情况
下面是有关路由知识的例子: 链接: https://pan.baidu.com/s/1nvj7gmd 密码:rsak
完成一个路由跳转的步骤:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |