網站搜尋

typescript - angular 在 RouteConfigLoadEnd 處理器中訪問 route._loadedConfig


我如何存取 route._loadedConfig 成員?它總是未定義。

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

這些都不起作用。

VS-Code 在除錯視窗中顯示值:

答案#1

假設你想存取 _loadedConfig 的 routes 對象(由於模組對象大多是內部的 Angular 內容,你實際上不應該隨意操作),我建議不要直接存取它,因為它不是公開 API 的一部分,因此可能會改變或不可靠。

要在載入時存取延遲加載模組的子路由,可以做如下操作:

建立一個 root 注入的服務

@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 );
  }
}