简体   繁体   English

Angular 6:没有服务和传递信息来配置模块的单例

[英]Angular 6: no singleton for service and pass info to configuration a module

My app has this structure: 我的应用程序具有以下结构:

src
 |-- modules
 |  |-- app2-module
 |  
 |-- shared-common
 |  |-- services
 |      |-- rest.services.ts
 |
 |-- app.module.ts

The rest.services has to be configurable. rest.services必须是可配置的。

If it called in app2 ( app2-module ) the get methods use, for example, rest/app2-api/getCompanyList ; 如果在app2( app2-module )中调用,则get方法将使用,例如rest/app2-api/getCompanyList ; if it called in app1 ( app.module.ts ) the get methods use, for example, rest/app1-api/getCompanyList . 如果它在app1( app.module.ts )中调用,则get方法将使用,例如rest/app1-api/getCompanyList

So I create a configurable shared module in this way 所以我以这种方式创建了一个可配置的共享模块

export default interface SharedCommonConfig {
    createApiUrl(api: string): string;
}

export const SharedCommonConfigService): = new InjectionToken<SharedCommonConfig>('SharedCommonConfig');

export class SharedCommonModule {
    static forRoot(config: SharedCommonConfig): ModuleWithProviders {
        return {
            ngModule: SharedCommonModule 
            providers: [
                {
                    provide: SharedCommonConfigService): Service,
                    useValue: config
                },
                RestService
            ],
        };
    }
}

and I pass the configuration in app.module.ts 我在app.module.ts传递了配置

@NgModule({

    imports: [
        BrowserModule,
        App2Module,
        SharedCommonModule .forRoot({
            createApiUrl: (api: string) => {    
                return `/rest/app1-app/${api}`;
            }
        })
    ],

})

export class AppModule implements OnInit {}

and in App2Module 并在App2Module中

@NgModule({

    imports: [
        BrowserModule,
         SharedCommonModule .forRoot({
            createApiUrl: (api: string) => {    
                return `/rest/app2-app/${api}`;
            }
        })
    ],

})

export class App2Module implements OnInit {}

App2Module is showed on screen when the user navigate to url http://example.com/#/app2/ and in this case the rest service has to call the api with /rest/app2-app as a prefix; 当用户导航到URL http://example.com/#/app2/时,屏幕上会显示App2Module,在这种情况下,其余服务必须以/rest/app2-app为前缀来调用api; but it's not work because read only the configuration in app.module.ts. 但这是行不通的,因为只能读取app.module.ts中的配置。

Is it possibile don't use the rest.service as singleton? 可以不将rest.service用作单例吗?

The action of importing the shared service in the root app.module in this way will make it a singleton. 以这种方式在根app.module中导入共享服务的操作将使它成为单例。

To avoid this, you would need to separate app1.module and app2.module and then register the service in both of those and not register it in the main app.module. 为避免这种情况,您需要将app1.module和app2.module分开,然后在这两个服务中注册该服务,而不是在主app.module中注册该服务。

Therefore, you would need: 因此,您需要:

@NgModule({

    imports: [
        BrowserModule,
        App1Module,
        App2Module
    ],

})
export class AppModule implements OnInit {}

... and then separate App1Module and App2Module both registering the shared module as you have done for App2Module: ...然后分开App1Module和App2Module都注册共享模块,就像对App2Module所做的那样:

App 1: 应用程式1:

@NgModule({

    imports: [
        BrowserModule,
        SharedCommonModule .forRoot({
            createApiUrl: (api: string) => {    
                return `/rest/app1-app/${api}`;
            }
        })
    ],

})
export class App1Module implements OnInit {}

App 2: 应用程式2:

@NgModule({

    imports: [
        BrowserModule,
        SharedCommonModule .forRoot({
            createApiUrl: (api: string) => {    
                return `/rest/app2-app/${api}`;
            }
        })
    ],

})
export class App2Module implements OnInit {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM