首先我们来创建一个新的项目:
nest new module-test -p npm
创建两个模块 我们就创建 模块 test1 和test2 吧
nest g module test1
nest g module test2
接着让两个模块相互引用:
import { Module } from '@nestjs/common';
import { Test2Module } from 'src/test2/test2.module';
@Module({
imports: [Test2Module]
})
export class Test1Module { }
import { Module } from '@nestjs/common';
import { Test1Module } from 'src/test1/test1.module';
@Module({
imports: [Test1Module]
})
export class Test2Module { }
把服务跑起来:
pnpm run start:dev
可以发现报错了
错误的原因是模块之间的依赖循环 报错的提示说了 我们可以使用 forwardRef() 来解决这个问题
forwardRef 在 NestJS 中用于解决依赖注入循环引用的问题。
如果 Test1Module 需要使用 Test2Module 中的一个服务,而 Test2Module 同样需要使用 Test1Module 中的服务,就会出现循环依赖。这会导致 NestJS 无法正确解析模块和它们之间的依赖关系。
修改代码如下;
test1.module.ts
import { Module, forwardRef } from '@nestjs/common';
import { Test2Module } from 'src/test2/test2.module';
@Module({
imports: [forwardRef(() => Test2Module)]
})
export class Test1Module {}
test2.module.ts
import { Module, forwardRef } from '@nestjs/common';
import { Test1Module } from 'src/test1/test1.module';
@Module({
imports: [forwardRef(() => Test1Module)]
})
export class Test2Module { }
再重新启动一下项目 可以发现启动成功不会报错:
pnpm run start:dev
forwardRef 的作用就是先进行创建 单独创建两个module 都创建成功之后再进行引入
除了Module 之间会循环依赖 provider 之间也会有循环引用的异常报错问题
我们先创建一下两个 service
nest g service test1 --no-spec
nest g service test2 --no-spec
接着让两者互相引入:
test1.service.ts
import { Injectable } from '@nestjs/common';
import { Test2Module } from 'src/test2/test2.module';
@Injectable()
export class Test1Service {
constructor(private test2Module:Test2Module) { }
}
test1.module.ts
import { Module, forwardRef } from '@nestjs/common';
import { Test2Module } from 'src/test2/test2.module';
import { Test1Service } from './test1.service';
import { Test2Service } from 'src/test2/test2.service';
@Module({
imports: [forwardRef(() => Test2Module)],
providers: [Test1Service, Test2Service]
})
export class Test1Module { }
test2.service.ts
import { Injectable } from '@nestjs/common';
import { Test1Service } from 'src/test1/test1.service';
@Injectable()
export class Test2Service {
constructor(private test1Module:Test1Service) { }
}
test2.module.ts
import { Module, forwardRef } from '@nestjs/common';
import { Test1Module } from 'src/test1/test1.module';
import { Test2Service } from './test2.service';
import { Test1Service } from 'src/test1/test1.service';
@Module({
imports: [forwardRef(() => Test1Module)],
providers: [Test2Service,Test1Service]
})
export class Test2Module { }
接着启动项目:
pnpm run start:dev
可以看到报错了 是循环依赖引起的报错
此时我们可以通过forwardRef 来解决循环依赖的问题
修改下面代码:
test1.service.ts
import { Inject, Injectable, forwardRef } from '@nestjs/common';
import { Test2Service } from 'src/test2/test2.service';
@Injectable()
export class Test1Service {
constructor(
@Inject(forwardRef(() => Test2Service)) // 更正这里,使用Test2Service而不是Test1Service
private test2Service: Test2Service,
) {}
test() {
return this.test2Service.test() + 'test1';
}
}
test2.service.ts
import { Inject, Injectable, forwardRef } from '@nestjs/common';
import { Test1Service } from 'src/test1/test1.service';
@Injectable()
export class Test2Service {
constructor(@Inject(forwardRef(() => Test1Service)) private test1Service: Test1Service) { }
test() {
return this.test1Service.test() + 'test2 调用'
}
}
再次重新启动可以发现 正常运行: