使用带路由器的解析器

时间:2018-01-18 11:39:04

标签: angular angular-routing angular5

我有两个不同的路径用于同一个组件,它们使用解析数据:

//app.routing:
const APP_ROUTES: Routes = [
{
    path: '',
    canActivate: [LoggedInGuard],
    children: [
        {path: '', redirectTo: '/path1', pathMatch: 'full'},
        {path: 'path1', loadChildren: 'app/myModule.module#MyModuleModule'},
        {path: 'path2', loadChildren: 'app/myModule.module#MyModuleModule'}  
    ]
},
{path: '**', redirectTo: ''}
];


export const routing = RouterModule.forRoot(APP_ROUTES);

在myModule.routing中:

const MY_MODULE_ROUTES: Routes = [
    {path: '', component: MyModule2Component, resolve: {data: MyModule2Resolver}}
];


export const myModuleRouting = RouterModule.forChild(MY_MODULE_ROUTES);

这一切都很好。

现在我想从MyModule2Resolver控制路由:

@Injectable()
export class  MyModule2Resolver implements Resolve<any[]> {
 constructor(private activatedRoute: ActivatedRoute, private router: Router) {}

 resolve(): Promise<any> {
      //Different action depending on path1 or path2 but
     // I have not this info in activatedRoute or router
 }
}

有可能吗?

1 个答案:

答案 0 :(得分:2)

您的activatedRoute包含一个属性url,其中包含有关url的信息。使用它并编写你的逻辑