gpt4 book ai didi

Nest.JS token providers(Nest.JS令牌提供程序)

转载 作者:bug小助手 更新时间:2023-10-28 09:34:54 25 4
gpt4 key购买 nike



I'm trying to provide a token to my module. The module looks like this:

我正试着给我的模块提供一个令牌。该模块如下所示:


@Module({
providers: [AuthGuard],
exports: [AuthGuard],
})
export class AuthGuardModule {
public static forRoot(
configProvider: Provider<IGuardConfigToken>
): DynamicModule {
return {
providers: [configProvider],
module: AuthGuardModule,
};
}
}

The parent module into which I import AuthGuardModule looks like this:

我将AuthGuardModule导入到的父模块如下所示:


@Module({
imports: [
TokenDataAccessModule,
AuthGuardModule.forRoot(guardConfigProvider),
],
controllers: [CompanyController],
})
export class CompanyModule {}

And the guardConfigProvider itself looks like this:

而gardConfigProvider本身如下所示:


export const guardConfigProvider: FactoryProvider<IGuardConfigToken> = {
provide: GUARD_CONFIG_TOKEN,
useFactory: (tokenService: TokenService): IGuardConfigToken => ({
validateToken: tokenService.validateToken.bind(tokenService),
}),
inject: [TokenService],
};

But I get an error:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the GUARD_CONFIG_TOKEN (?). Please make sure that the argument TokenService at index [0] is available in the AuthGuardModule context.

但我得到一个错误:Error[ExceptionHandler]Nest无法解析Guard_CONFIG_TOKEN(?)的依赖项。请确保索引[0]处的参数TokenService在AuthGuardModule上下文中可用。


TokenService is located in the TokenDataAccessModule provider and is exported there. I don't understand why this error occurs in this case.

TokenService位于TokenDataAccessModule提供程序中,并在那里导出。我不明白为什么在这种情况下会发生这个错误。


It seems that in the example above the provisioning should work, but it doesn’t

在上面的示例中,配置似乎应该起作用,但它不起作用


更多回答
优秀答案推荐

Because AuthGuardModule has the provider

因为AuthGuardModule具有提供程序


{
provide: GUARD_CONFIG_TOKEN,
useFactory: (tokenService: TokenService): IGuardConfigToken => ({
validateToken: tokenService.validateToken.bind(tokenService),
}),
inject: [TokenService],
}

the inject tells Nest that there needs to be access to theTokenService provider in the AuthGuardModule. Adding the TokenDataAccessModule to the imports array of the dynamic module for the AuthGuardModule will resolve the issue for you.

注入告诉Nest需要访问AuthGuard模块中的TokenService提供程序。将TokenDataAccessModule添加到AuthGuardModule的动态模块的Imports数组中将为您解决该问题。


更多回答

If I add a TokenDataAccessModule to the AuthGuardModule, then the meaning of my token is lost. In AuthGuardModule I would like to use only GUARD_CONFIG_TOKEN

如果我向AuthGuardModule添加一个TokenDataAccessModule,那么我的令牌就失去了意义。在AuthGuardModule中,我只想使用Guard_CONFIG_TOKEN

But your GUARD_CONFIG_TOKEN relies on TokenService so wherever you create the GUARD_CONFIG_TOKEN provider, you need to have access to TokenService and for that you need to import TokenDataAccessModule, that's literally how Nest's modules and providers work

但是您的GUARD_CONFIG_TOKEN依赖于TokenService,因此无论您在何处创建GUARD_CONFIG_TOKEN提供程序,您都需要访问TokenService,为此您需要导入TokenDataModule,这就是Nest的模块和提供程序的工作方式

What's the point of creating tokens then if I can't use dependencies on the modules on top? Doesn't nest.js have a tree of injectors like Angular?

如果我不能在上面的模块上使用依赖项,那么创建令牌有什么意义呢?nest.js不是有一个像Angular一样的注入器树吗?

Providers don't traverse "top-down", they traverse through the imports of a module, "up" the chain so to speak.

提供者不遍历“自上而下”,他们遍历模块的导入,可以说是“向上”链。

If we imagine that AuthGuardModule is a module from a third-party library, for its correct operation you need to provide the GUARD_CONFIG_TOKEN token with the validateToken method. How can I do that?

如果我们假设AuthGuardModule是来自第三方库的模块,为了使其正确操作,您需要使用valiateToken方法提供Guard_CONFIG_TOKEN。我怎么能做到这一点?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com