代码之家  ›  专栏  ›  技术社区  ›  char m

如何从库扩展角度服务,以便将在同一个库中定义的构造函数参数类型传递给它?

  •  0
  • char m  · 技术社区  · 6 年前

    我正在尝试扩展多窗口服务 https://www.npmjs.com/package/ngx-multi-window :

    import { Injectable } from '@angular/core';
    import { Location } from '@angular/common';
    import { MultiWindowService, MultiWindowConfig, StorageService, WindowRef } from 'ngx-multi-window';
    import { Router } from '@angular/router';
    import { AuthService } from './auth.service';
    
    @Injectable({
      providedIn: 'root'
    })
    export class MyMultiwindowService extends MultiWindowService{
      public myExtraThing: boolean;
    
      constructor(location: Location, storageService: StorageService, windowRef: WindowRef, router: Router, authService: AuthService, customConfig: MultiWindowConfig) { 
        super(customConfig, location, storageService, windowRef);
        // do some extra stuff
    }
    

    但是,最后一个参数在生成时会导致问题--aot:

    customConfig: MultiWindowConfig 
    

    警告:无法在C/dev/web/multiwindowproto/src/app/my-multiwindow.service.ts中解析MyMultiwindowService的所有参数:([对象对象],[对象对象],[对象对象],[对象对象],?).

    在app.module中我有:

    // core etc imports omited
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { MapviewComponent } from './mapview/mapview.component';
    import { ContentComponent } from './content/content.component';
    import { ThreedeegraphicsComponent } from './threedeegraphics/threedeegraphics.component';
    
    import { MultiWindowConfig, MultiWindowModule, WindowSaveStrategy, NGXMW_CONFIG } from 'ngx-multi-window';
    
    const config: MultiWindowConfig = {windowSaveStrategy: WindowSaveStrategy.SAVE_WHEN_EMPTY};
    
    @NgModule({
      declarations: [
        AppComponent,
        MapviewComponent,
        ContentComponent,
        ThreedeegraphicsComponent
      ],
      imports: [
        BrowserModule,
        FormsModule, ReactiveFormsModule,
        MultiWindowModule.forRoot(config),
        AppRoutingModule,
    
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { 
    }
    

    那么如何定义/提供扩展服务的配置呢?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Chirag Patel    6 年前

    这是因为尚未创建MultiWindowConfig对象。你需要有 多窗口配置 在你的 提供者 如果是的话 @可注射的 . 如果没有,可以使用工厂模型创建对象( https://angular.io/guide/dependency-injection-providers ). 首先为类创建工厂和提供程序:

    const MyMultiwindowServiceFactory = (location: Location, storageService: StorageService, windowRef: WindowRef, router: Router, authService: AuthService) => {
        const config: new MyMultiWindowConfig();
        return new MyMultiwindowService(location, storageService, windowRef, router, authService, config)
    }
    
    export let MyMultiwindowServiceProvider = {
       provide: MyMultiwindowService,
       useFactory: MyMultiwindowServiceFactory,
       deps: [Location, StorageService, WindowRef, Router, AuthService]
    }
    

    然后在你的 应用模块.ts 您将导入提供商并提供它:

    import {MyMultiwindowServiceProvider } from './services/my-multi-window.service.ts'
        @NgModule({
      .....other code
      providers: [MyMultiwindowServiceProvider, ....other services],
      bootstrap: [AppComponent]
    })
    

    现在每当你提到 我的多窗口服务 提供程序将使用您在工厂中设置的配置提供服务的实例。

    推荐文章