生命周期事件
优质
小牛编辑
128浏览
2023-12-01
生命周期事件
There are two module lifecycle events OnModuleInit and OnModuleDestroy. You should use them for all the initialization stuff and avoid to work in the constructor. The constructor should only be used to initialize class members but nothing more. Example:
有两个生命周期事件OnModuleInit
和OnModuleDestroy
。你应该使用它们来初始化所有的内容,从而避免在构造函数中初始化。因为构造函数只用于初始化类成员。
示例:
import { OnModuleInit, OnModuleDestroy } from '@nestjs/common';
@Component()
export class UsersService implements OnModuleInit, OnModuleDestroy {
onModuleInit() {
console.log('Module initialized...');
}
onModuleDestroy() {
console.log('Module destroyed...');
}
}