【问题标题】:Inject component(Service) into other component(Service) using module exports使用模块导出将组件(服务)注入其他组件(服务)
【发布时间】:2018-09-14 17:27:40
【问题描述】:

我已阅读有关hierarchical inject 的 Nestjs 文档中的文章。但我在实施它时遇到了问题。目前,我有两个模块。 AuthModule 是导入 UserModule 的模块。 UserModule 包含在其他模块中使用的服务(它可以正常工作)。

auth.module.ts

import * as passport from 'passport';
import {
  Module,
  NestModule,
  MiddlewaresConsumer,
  RequestMethod,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import {JwtStrategy} from './jwt.straegy';

import {UserModule} from '../shared/user/user.module';

@Module({
  imports: [UserModule],
  components: [AuthService, JwtStrategy],
  exports: [AuthService]
})
export class AuthModule implements NestModule {
  public configure(consumer: MiddlewaresConsumer) {
    consumer
      .apply(passport.authenticate('jwt', { session: false }))
      .forRoutes({ path: '/cats', method: RequestMethod.ALL });
  }
}

user.module.ts

import {Module} from '@nestjs/common';
import {MongooseModule} from '@nestjs/mongoose';
import {UserSchema} from '../../schema/user.schema';
import {UserService} from './user.service';

@Module({
  imports: [
    MongooseModule.forFeature([{ name: 'UserInterface', schema: UserSchema }])
  ],
  components: [UserService],
  exports: [UserService]
})
export class UserModule {}

当nestjs 将UserService 注入到AuthService 时,问题就出现了。遇到以下错误。从构造函数中删除服务是可行的。奇怪的是 UserService 可以在其他模块中工作。

Error: Nest can't resolve dependencies of the AuthService (?). Please verify whether [0] argument is available in the current context.

auth.service.ts

import * as jwt from 'jsonwebtoken';
import { Component } from '@nestjs/common';
import {UserService} from '../shared/user/user.service';

@Component()
export class AuthService {

  constructor(private readonly userService: UserService) {}

  async createToken(username) {
    const expiresIn = 60 * 60, secretOrKey = 'i am fake!';
    const user = {username};
    const token = jwt.sign(user, secretOrKey, {expiresIn});
    return {
      expires_in: expiresIn,
      access_token: token
    };
  }

  async validateUser(signedUser): Promise<boolean> {
    return !!(await this.userService.findByUsername(signedUser.username));
  }
}

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

    这很奇怪,我确实有几乎相同的代码可以工作,我想我注意到了,我不确定这是否是这里的问题,但您可以在 anther 模块中使用 authService 吗?如果是这样,您可能需要在 authModule 中重新导出 usersModule,以便您当前的范围拥有它:

    在 authModule 中:

    exports: [UserModule, AuthService]
    

    (无法显示完整的示例,因为 stackoverflow 被唤醒并且错误的脚并且不允许我添加代码块)

    如果此更改有效,您可能已经“导入”(添加到组件数组)authService 到另一个模块而不导入整个 authModule, 如果你能提供所有代码的 github repo 会更容易。

    记住,authService 现在是 authModule 的一部分,不应该直接添加到另一个模块,而只能通过“导入”数组,如果不是你的情况,你可以向 github repo 提供所有代码,它可能更容易帮助

    【讨论】:

    • 所以回复这么晚,但迟到总比没有。看来我的代码(nestjs 入门项目中提供的默认代码)已经过时了,因此当我尝试使用该服务时它不起作用。当我尝试使用最新的库时,它可以工作。感谢您的帮助!
    • 是的启动项目和 CLI 里面有一些旧的东西,我也有很多相关的问题
    猜你喜欢
    • 2019-05-29
    • 2017-01-29
    • 2016-02-10
    • 2019-11-03
    • 1970-01-01
    • 2018-08-22
    • 2018-05-20
    • 2019-06-15
    相关资源
    最近更新 更多