RouteConfigLoadEnd-Handler 中的 Angular 访问 route._loadedConfig

标签 angular typescript angular-routing

如何访问 route._loadedConfig 成员?它始终未定义。

const a = (route as any)._loadedConfig;
const a =  (<any>route )._loadedConfig;
const a =  (<any>route )['_loadedConfig'];

这些都不起作用。

VS-Code 在调试窗口中显示值:

debug window

最佳答案

假设你想访问 _loadedConfigroutes 对象(因为模块对象主要是内部 Angular 东西你不应该真的乱七八糟)我会建议不要直接访问它,因为它不是公共(public) API 的一部分,因此可能会更改或不可靠。

要在加载时访问延迟加载模块的子根,您可以执行如下操作:

创建根注入(inject)服务

@Injectable({
  providedIn: 'root'
})
export class LazyLoadedChildRouteService {
    public updateLazyLoadedRoutes( childRoutes: Routes ){ ... }
    // CONSTRUCTOR
    constructor( ... ){ ... }
}

在模块文件中:

export const lazyLoadedRoutes: Routes = [
    {
      path: 'lazyRoute',
      component: LazyLoadedComponent
    },
]

@NgModule({
  ...
  imports: [
    RouterModule.forChild( lazyLoadedRoutes ),
  ],
  ...
})
export class LazyLoadedModule {
  /** CONSTRUCTOR */
  constructor(
    public $translateUrlService: FgTranslateRouterService
  ) {
    this.$translateUrlService.addChildModuleRoutes( lazyLoadedRoutes );
  }
}

关于RouteConfigLoadEnd-Handler 中的 Angular 访问 route._loadedConfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299454/

相关文章:

youtube-api - 来自 DefinitelyTyped 的 TypeScript youtube.d.ts

json - 如何为 Angular 6 中的下拉功能迭代多嵌套 JSON 对象?

angular - 特定组件 Angular 的动态标题

angular - 路由器可以激活超过 1 个守卫

Angular 4 路由器 : direct browser navigation restarts app

javascript - PDFTron : How to add an input field

angular - 如何从 Angular Material 日期选择器中排除日期?

javascript - 为事件处理程序编写中间件

javascript - 使用 SystemJS 将 ng2-ace 库集成到新创建的 angular-cli (angular2) 项目中

angular - 当我切换到另一个页面时 bootstrap-select 消失