javascript - Angular:了解 DI 如何在动态加载的组件中工作

标签 javascript angular

我遇到了向动态加载的组件注入(inject)服务的奇怪行为。考虑以下服务

@Injectable({
    providedIn: 'root'
})
export class SomeService {
    private random = Math.random() * 100;

    constructor() {
        console.log('random', this.random);
    }
}

该服务被添加为两个组件的依赖项。第一个组件是延迟加载模块的一部分。而第二个是动态加载的。以下服务使用动态组件加载模块

export const COMPONENT_LIST = new InjectionToken<any>('COMPONENT_LIST');
export const COMPONENT_TYPE = new InjectionToken<any>('COMPONENT_TYPE');

@Injectable({
    providedIn: 'root'
})
export class LoaderService {
    constructor(
        private injector: Injector,
        private compiler: Compiler,
    ) { }

    getFactory<T>(componentId: string): Observable<ComponentFactory<T>> {
        // COMPONENT_LIST is passed through forRoot() from the module that declares first component
        const componentList = this.injector.get(COMPONENT_LIST); 
        const m = componentList.find(m => m.componentId === componentId);

        const promise: Promise<ComponentFactory<T>> = (!m) ? null :
            m.loadChildren
                .then((mod: any) => {
                    return this.compiler.compileModuleAsync(mod);
                })
                .then((mf: NgModuleFactory<any>) =>  {
                    const mr: NgModuleRef<any> = mf.create(this.injector);
                    const type: Type<T> = mr.injector.get<Type<T>>(COMPONENT_TYPE); // DYNAMIC_COMPONENT is provided in loaded module
                    return mr.componentFactoryResolver.resolveComponentFactory<T>(dynamicComponentType);
                });

        return from(promise);
    }
}

在同一个模块中,我声明了以下组件(用于放置动态加载的组件)和指令

@Component({
    selector: 'dynamic-wrapper',
    template: `<ng-container dynamicItem></ng-container>`
})
export class DynamicWrapperComponent implements AfterViewInit, OnDestroy {
    @Input() itemId: string;
    @Input() inputParameters: any;

    @ViewChild(DynamicItemDirective)
    private dynamicItem: DynamicItemDirective;

    private unsubscribe$: Subject<void> = new Subject();

    constructor(
        private loaderService: LoaderService
    ) { }

    ngAfterViewInit(): void {
        this.loaderService.getComponentFactory(this.itemId).subscribe((cf: ComponentFactory<any>) => {
            this.dynamicItem.addComponent(cf, this.inputParameters);
        });
    }
}
...
@Directive({
    selector: '[dynamicItem]'
})
export class DynamicItemDirective {

    constructor(protected viewContainerRef: ViewContainerRef) { }

    public addComponent(cf: ComponentFactory<any>, inputs: any): void {
        this.viewContainerRef.clear();
        const componentRef: ComponentRef<any> = this.viewContainerRef.createComponent(cf);
        Object.assign(componentRef.instance, inputs);
        // if I do not call detectChanges, ngOnInit in loaded component will not fire up
        componentRef.changeDetectorRef.detectChanges();
    }
}

SomeService 定义在一个单独的模块中,该模块在具有第一个组件的延迟加载模块和动态加载模块中导入。

在初始化两个组件后,我在控制台中看到了 console.log('random', this.random) 的输出,尽管 providedIn: 'root' 在装饰器中。这种奇怪行为的原因是什么?

最佳答案

延迟加载的模块不会对 providedIn: 'root' 使用react正如人们所料。 仅当导入服务的模块不是延迟加载时,此选项才会将服务推送到 AppModule(根模块)。

为什么会看到不同的随机数?

因为 SomeService 的模块被定义在被启动了两次! (随意将 console.log 放在这个模块的构造函数中) - 因此服务被启动了两次(我自己在我的项目延迟加载平台中遇到了这个问题)。

为什么惰性模块包含SomeService启动两次?

loadChildrenmoduleFactory.create(this.injector)不要保留延迟加载模块的缓存。他们启动一个模块并将其注入(inject)器作为叶子附加到输入注入(inject)器。

如果模块包含SomeServicemoduleFactory.create 创建- 你可以添加一个缓存层来返回缓存启动的延迟加载模块。

例如:

private lazyLoadedModulesCache: Map<Type<any>, NgModuleRef<any>>;

关于javascript - Angular:了解 DI 如何在动态加载的组件中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65771898/

相关文章:

javascript - 最近 24 小时数据报告显示在 Chart.Js 中

c# - 参数复杂对象作为未定义进入 JavaScript 函数

javascript - 在单引号 (') 前添加值

angular - 如何增加 mat-menu-item 的宽度?

javascript - Puppeteer 转到不同的页面

javascript - 两个音频文件一一播放

angular - 指令 QuillEditorBase 中的 Ngx-Quill 错误

javascript - ASP.NET MVC 和 Angular 4 mini SPA - 页脚问题

javascript - 如何在 Angular 5 中验证复选框

angular - 将值绑定(bind)到样式