第十章 Nest 解决Module和Provider循环依赖问题


首先我们来创建一个新的项目:

nest new module-test -p npm

1717332805855.png
创建两个模块 我们就创建 模块 test1 和test2 吧

nest g module test1
nest g module test2

1717333373644.png
接着让两个模块相互引用:

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 { }

1717333819887.png
1717333848802.png
把服务跑起来:

pnpm run start:dev

可以发现报错了
1717333945389.png
1717334007233.png
错误的原因是模块之间的依赖循环 报错的提示说了 我们可以使用 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 {}

1717852805055.png
test2.module.ts

import { Module, forwardRef } from '@nestjs/common';
import { Test1Module } from 'src/test1/test1.module';

@Module({
    imports: [forwardRef(() => Test1Module)]
})
export class Test2Module { }

1717852837436(1).png
再重新启动一下项目 可以发现启动成功不会报错:

pnpm run start:dev

1717853259429.png

forwardRef 的作用就是先进行创建 单独创建两个module 都创建成功之后再进行引入

除了Module 之间会循环依赖 provider 之间也会有循环引用的异常报错问题
我们先创建一下两个 service

nest g service test1 --no-spec 
nest g service test2 --no-spec

1717854270610.png
接着让两者互相引入:
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 { }

1717856211426.png
1717856241854.png
接着启动项目:

pnpm run start:dev

1717856465449.png
可以看到报错了 是循环依赖引起的报错
1717857722075.png
此时我们可以通过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 调用'
    }
}

再次重新启动可以发现 正常运行:
1717857938337.png

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫ゞ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值