当前模块使用其他模块 service 方式
-
将其他模块的
module
导入到 当前模块module
文件中的imports
中 -
在当前模块需要的地方,比如
controller
或service
文件中引入其他模块的service
,并在对应文件的constructor
函数参数中使用private readonly otherService: otherService
的方式注入 -
在其他路由方法中即可使用
this.otherService
的方式调用其他模块service
中定义的方法
示例
Chat 模块
chat-service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class ChatService {
getHello(): string {
return 'Hello World!';
}
}
chat-controller.ts
import { Controller, Get } from '@nestjs/