简体   繁体   中英

Globle module without import not working nestjs

I am trying to implement a global module in nest.js

I have created a service like below

export interface ConfigData {
    DB_NAME:        string;
}

@Injectable()
export class ConfigManager {
    private static _instance?: ConfigManager;
    public static configData:ConfigData | null;
    private constructor() {
        console.log(ConfigManager)
        if (ConfigManager._instance)
            ConfigManager._instance = this;
        else{
            ConfigManager._instance = new ConfigManager()
            ConfigManager.configData = <ConfigData|null><unknown>ConfigManager.fetchSecretData()
        }
    }
    private static async fetchSecretData():Promise<ConfigData|null>{
        // some data from db
    }
    // static get instance() {
    //     return ConfigManager._instance ?? (ConfigManager._instance = new ConfigManager());
    //     //return ConfigManager._instance ?? (ConfigManager._instance = ConfigManager.fetchSecretData()) //new ConfigManager());
    // }
    
}

configuration.module.ts

@Global()
@Module({
    providers: [ConfigManager],
    exports: [ConfigManager],
})
export class ConfigurationModule {}

and in app.module.ts added ConfigurationModule in imports.

Also adding private constructor on service unable it to add in module.ts file.

I am expecting that I should be able to configData anywhere without importing the ConfigManager. but it's not working...

ConfigManager is not available without import.

You've only marked your module with @Global decorator, but the NestJS needs to somehow initialize that module and make it globally available.

What this means is that you have to add this module to your core application module and NestJS will do the rest for you, so something like this (or however your root module is named):

@Module({
  imports: [ConfigurationModule],
})
export class AppModule {}

From the documentation

The @Global() decorator makes the module global-scoped. Global modules should be registered only once, generally by the root or core module.

this is 'per-design' of es6/ts: you cant use the class without importing it.

you are mixing the concepts of di (instantiation/composition) with importing (defining which classes are available in the module scope)

Global modules in nest only means you don't have to include that module in the imports: [] of every other module that requires it's providers. The providers in the global module still behaves as as normal providers ie you need to inject it where you need it.

So in your case since you have already added @Global() to ConfigManager and imported ConfigurationModule in app.module.ts , you will not need to add ConfigurationModule in imports for any other modules who want to use ConfigManager . You would, however, still need to inject the ConfigManager provider - that's what @Injectable() means:).

To do the injection, you need a constructor(private configManager: ConfigManager) {} in the class that needs to use ConfigManager , and since you need access to the type of the class, you'll need import { ConfigManager } from '../path/to/ConfigManager' as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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