如何为SystemJsNgModuleLoader设置工厂路径前缀?

时间:2018-09-04 09:54:08

标签: angular

我将SystemJsNgModuleLoader用于延迟加载模块:

    export function createJitCompiler() {
        return new JitCompilerFactory().createCompiler([{useJit: true}]);
    }

    export function createLoader(_compiler: Compiler) {
        return new ModuleLoader(_compiler);
    }

    export class ModuleLoader implements NgModuleFactoryLoader {
    private systemJsNgModuleLoader: SystemJsNgModuleLoader;

    constructor(
        private readonly compiler: Compiler,
        @Optional() config?: SystemJsNgModuleLoaderConfig
    ) {
        this.systemJsNgModuleLoader = new SystemJsNgModuleLoader(compiler, {
            factoryPathPrefix: "js",
            factoryPathSuffix: ""
        });
    }

    public load(path: string) {
        return this.systemJsNgModuleLoader.load(path);
    }
}

    @NgModule({
        imports: [
            BrowserModule,
            CoreModule.forRoot()
        ],
        providers: [
        {provide: Compiler, useFactory: createJitCompiler},
        {provide: NgModuleFactoryLoader, useClass: ModuleLoader},
            provideRoutes([
                {loadChildren: "../modules/test/test.module#TestModule"}
            ])
        ]
    })
    export class AppModule {

        constructor(private injector: Injector,
                    private moduleLoader: NgModuleFactoryLoader,
                    private compiler: Compiler) {
        }

        ngDoBootstrap(appRef: ApplicationRef) {
            this.moduleLoader.load("../modules/test/test.module#TestModule").then((moduleFactory: NgModuleFactory<any>) => {
                this.compiler.compileModuleAndAllComponentsAsync(moduleFactory.moduleType)
                    .then((compiled) => {
                        //TODO
                    })
            });
        }

    }

AOT编译后,我的文件包含在/js目录中,但是moduleLoader 尝试从站点根目录而不是从/js文件夹下载模块。我的班级ModuleLoader无法将SystemJsNgModuleLoaderConfig设置为班级SystemJsNgModuleLoader,以设置用于加载模块的根目录。

告诉我如何为SystemJsNgModuleLoaderConfig设置SystemJsNgModuleLoader

0 个答案:

没有答案