angular - 将解析器与路由器一起使用

标签 angular angular-routing angular5

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

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

可能吗?

最佳答案

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

关于angular - 将解析器与路由器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48320446/

相关文章:

angular - {skipLocationChange : true} in router. 导航不起作用;改变状态

angular - 在 Angular 自定义错误处理程序中使用服务

angular - 延迟 observable 返回值,直到我从 Angular 5 的 HTTP 请求中获得结果

javascript - 如何在 Angular 5中同步调用http请求

javascript - 从 GeoFire 对象数组中删除重复项

angular5 - Angular 5/6项目查询参数加密

angular - 检查用户名是否存在于 Angular4 & GO

Angular Promise 不记录未捕获的异常

angular - Ngx-quill 工具栏自定义不起作用 - quill 无法导入模块

html - AngularJS 风格不适用于新环境