Javascript 导入后如何使用全局模块?
我遵循了关于如何创建基本配置服务的示例 在本教程的底部,您可以选择全局声明它: 您也可以将Javascript 导入后如何使用全局模块?,javascript,node.js,typescript,nestjs,Javascript,Node.js,Typescript,Nestjs,我遵循了关于如何创建基本配置服务的示例 在本教程的底部,您可以选择全局声明它: 您也可以将ConfigModule声明为全局模块,而不是在所有模块中重复导入ConfigModule 因此,以下是我的全局模块文档: 从@nestjs/common导入全局到配置模块 将@Global()装饰器添加到ConfigModule 已将ConfigModule导入AppModule 将ConfigModule添加到imports数组中 那么接下来呢?我曾尝试将ConfigService注入AppServ
ConfigModule
声明为全局模块,而不是在所有模块中重复导入ConfigModule
因此,以下是我的全局模块文档:
- 从
导入@nestjs/common
到全局
配置模块
- 将
装饰器添加到@Global()
ConfigModule
- 已将
导入ConfigModule
AppModule
- 将
添加到ConfigModule
数组中imports
ConfigService
注入AppService
中,但无法解决此问题
app.module.ts:
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { ConfigModule } from '../config/config.module';
@Module({
imports: [
ConfigModule,
],
controllers: [
AppController,
],
providers: [
AppService,
],
})
export class AppModule {}
应用程序服务.ts:
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
private readonly config: ConfigService;
constructor(config: ConfigService) {
this.config = config;
}
getHello(): string {
return config.get('DB_NAME');
}
}
import { Module, Global } from '@nestjs/common';
import { ConfigService } from './config.service';
@Global()
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`${process.env.NODE_ENV}.env`),
},
],
exports: [
ConfigService,
],
})
export class ConfigModule {}
import * as dotenv from 'dotenv';
import * as fs from 'fs';
export class ConfigService {
private readonly envConfig: { [key: string]: string };
constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(filePath));
}
get(key: string): string {
return this.envConfig[key];
}
}
配置模块.ts:
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
private readonly config: ConfigService;
constructor(config: ConfigService) {
this.config = config;
}
getHello(): string {
return config.get('DB_NAME');
}
}
import { Module, Global } from '@nestjs/common';
import { ConfigService } from './config.service';
@Global()
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`${process.env.NODE_ENV}.env`),
},
],
exports: [
ConfigService,
],
})
export class ConfigModule {}
import * as dotenv from 'dotenv';
import * as fs from 'fs';
export class ConfigService {
private readonly envConfig: { [key: string]: string };
constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(filePath));
}
get(key: string): string {
return this.envConfig[key];
}
}
配置服务.ts:
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
private readonly config: ConfigService;
constructor(config: ConfigService) {
this.config = config;
}
getHello(): string {
return config.get('DB_NAME');
}
}
import { Module, Global } from '@nestjs/common';
import { ConfigService } from './config.service';
@Global()
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`${process.env.NODE_ENV}.env`),
},
],
exports: [
ConfigService,
],
})
export class ConfigModule {}
import * as dotenv from 'dotenv';
import * as fs from 'fs';
export class ConfigService {
private readonly envConfig: { [key: string]: string };
constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(filePath));
}
get(key: string): string {
return this.envConfig[key];
}
}
我希望能够插入
ConfigService
并从任何模块访问它。您在AppService
中缺少此
限定符:
getHello(): string {
return this.config.get('DB_NAME');
^^^^^
}
此外,缺少导入:
import { ConfigService } from './config/config.service';
它表示您不需要多次导入(和注册)
ConfigModule
;只需在AppModule
中使用一次。然而,ConfigService
必须在使用(注入)它的任何地方导入。这更有意义,谢谢。最后一点的措辞把我弄糊涂了。“之后,CatsService提供程序将无处不在,尽管不会导入CatsModule。”只有提供程序是无处不在的。谢谢