Angular 4 で開発された既存のプロジェクトがあります。ユーザー権限に基づいて特定のルートへのアクセスを制御する必要があります。単純化されたルート構成は次のようになります。
[
{ path: '', redirectTo: '/myApp/home(secondary:xyz)', pathMatch: 'full' },
{ path: 'myApp'
children: [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', ... },
...
{ path: 'product'
children: [
{ path: '', redirectTo: 'categoryA', pathMatch: 'full' },
{ path: 'categoryA', component: CategoryAComponent, canActivate: [CategoryACanActivateGuard]},
{ path: 'categoryB', component: CategoryBComponent},
...
]
},
...
]
},
...
]
ここで、へのアクセスを制御したいと思いますwww.myWeb.com/myApp/product/categoryA
。ユーザーに十分な権限がない場合、ユーザーは にリダイレクトされ... /product/CategoryB
ます。CanActivate
これを行うために RouteGuard を作成しました。ガード クラスは次のようになります。
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute } from '@angular/router';
import { MyService } from '... my-service.service';
@Injectable()
export class CategoryACanActivateGuard implements CanActivate {
constructor(private myService: MyService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return this.myService.checkPermission()
.then(result => {
if (!result.hasAccess) {
//redirect here
this.router.navigate(['./myApp/product/categoryB']);
//works, but I want to remove hardcoding 'myApp'
//this.router.navigate(['../../categoryB']);
//doesn't work, redirects to home page
//this.router.navigate(['./categoryB'], { relativeTo: this.route});
//do not have this.route object. Injecting Activated route in the constructor did not solve the problem
}
return result.hasAccess;
});
}
}
すべて正常に動作しますが、次のようにターゲット ルートに相対的にリダイレクトする必要があります。
this.router.navigate(['/product/categoryB'], { relativeTo: <route-of-categoryA>});
// or
this.router.navigate(['/categoryB'], { relativeTo: <route-of-categoryA>});
残念ながら、オブジェクトrelativeTo
のみを受け入れActivatedRoute
、私が持っているのはActivatedRouteSnapshot
andだけですRouterStateSnapshot
。ターゲット ルート (この場合はcategoryA )に対してナビゲートする方法はありますか? どんな助けでも本当に感謝します。
ノート:
- いくつかのルート ガードを追加する以外に、ルート構成を変更できません。
this.router.navigateByUrl
を使用して探しているわけではありませんstate.url
。使いたいですrouter.navigate([...], { relativeTo: this-is-what-need})
。