小编yot*_*y66的帖子

Nestjs 中的提供者和导入有什么区别?

我正在关注 NestJs身份验证教程

\n

在 auth/auth.module.ts 中:

\n
import { JwtModule } from \'@nestjs/jwt\'\n\n\n...\n\n@Module({\n  imports: [\n\n    UsersModule,\n    PassportModule,\n    JwtModule.register({\n      secret: jwtConstants.secret,\n      signOptions: { expiresIn: \'60s\' },\n    }),\n  ],\n  providers: [AuthService, LocalStrategy, JwtStrategy],\n  exports: [AuthService],\n})\n
Run Code Online (Sandbox Code Playgroud)\n

因此 JwtModule 是在 \'imports\' 下导入的。

\n

在 auth/auth.service.ts 中注入 JwtService

\n
...\nimport { JwtService } from \'@nestjs/jwt\';\n\n@Injectable()\nexport class AuthService {\n  constructor(\n    private usersService: UsersService,\n    private jwtService: JwtService\n  ) {}\n\n...\n
Run Code Online (Sandbox Code Playgroud)\n

来自 NestJs 文档:

\n

在模块下:\nproviders - 将由 Nest 注入器实例化并且至少可以在该模块中共享的提供程序

\n
imports -   the list of …
Run Code Online (Sandbox Code Playgroud)

javascript node.js nestjs

11
推荐指数
1
解决办法
7851
查看次数

标签 统计

javascript ×1

nestjs ×1

node.js ×1