简体   繁体   English

导入 json 值时缺少 @Injectable 注释

[英]Missing @Injectable annotation when importing json value

I'm currently trying to inject a json file into a service that I am building.我目前正在尝试将 json 文件注入我正在构建的服务中。 This class is injecting the json via a @inject() tag in the constructor.这个 class 通过构造函数中的@inject()标记注入 json。 Below you can find the code that I'm using.您可以在下面找到我正在使用的代码。

DiContainer.ts DiContainer.ts

import { Container, decorate, injectable } from "inversify";
import { IResponseBuilder, InjectionTypes, IUserIdGenerator, ILogger, IResponseRepository, IResponseService, IContextService, IFileWriter, IBoardSessionService, IIntent } from "./src/common/types";
import GoogleResponseBuilder from "./src/common/builders/googleResponseBuilder";
import UserIdGenerator from "./src/common/helpers/userIdGenerator";
import { DialogflowConversation } from "actions-on-google";
import WinstonLogger from "./src/common/winstonLogger";
import FileWriter from "./src/common/helpers/fileWriter";
import TextResponseRepository from "./src/repositories/textResponseRepository";
import SsmlResponseRepository from "./src/repositories/ssmlResponseRepository";
import ResponseSerivce from "./src/services/responseService";
import ContextService from "./src/services/contextService";
import { BoardService } from "./src/services/boardService";
import { BoardSessionService } from "./src/services/boardSessionService";
import WelcomeIntent from "./src/intents/new/welcomeIntent";
import uuid from "uuid/v4";

const sessionJson = require("./src/data/boardSessions.json");



const DIContainer = new Container();

DIContainer.bind<IResponseBuilder<DialogflowConversation>>(InjectionTypes.GoogleResponseBuilder).to(GoogleResponseBuilder);
DIContainer.bind<ILogger>(InjectionTypes.WinstonLogger).to(WinstonLogger);
DIContainer.bind<IFileWriter>(InjectionTypes.FileWriter).to(FileWriter);
DIContainer.bind<IUserIdGenerator>(InjectionTypes.UserIdGenerator).to(UserIdGenerator);

DIContainer.bind<IIntent>(InjectionTypes.WelcomeIntent).to(WelcomeIntent);

DIContainer.bind<IResponseRepository>(InjectionTypes.TextResponseRepository).to(TextResponseRepository);
DIContainer.bind<IResponseRepository>(InjectionTypes.SsmlResponseRepository).to(SsmlResponseRepository);
DIContainer.bind<IResponseService>(InjectionTypes.ResponseService).to(ResponseSerivce);
DIContainer.bind<IBoardSessionService>(InjectionTypes.BoardSessionService).to(BoardSessionService);
DIContainer.bind<IContextService>(InjectionTypes.ContextService).to(ContextService);

DIContainer.bind(InjectionTypes.SessionJSON).to(sessionJson);
DIContainer.bind(InjectionTypes.UUIDv4).toFunction(uuid);


export default DIContainer;

Every dependency you try to inject via @inject has to be marked with @injectable and registered through bind.您尝试通过@inject 注入的每个依赖项都必须用@injectable 标记并通过绑定注册。 I don't really know how the decorate function works (so I don't know if you can use it to mark a function, and not a class, as injectable).我真的不知道装饰 function 是如何工作的(所以我不知道你是否可以用它来标记 function,而不是 class,作为可注射)。

By the way, I think you can achieve what you want just registering your dependency as a dynamic value and returning the desired function, as stated here .顺便说一句,我认为您可以实现您想要的,只需将您的依赖项注册为动态值并返回所需的 function,如此所述。 In your case, something like this:在你的情况下,是这样的:

DIContainer.bind(InjectionTypes.UUIDv4).toDynamicValue((context: interfaces.Context) => { return uuid });

Alternatively, you could just directly import the function in your service without injecting it, or wrap the function in another service you can mark as injectable (let say, an uuid provider service).或者,您可以直接在您的服务中导入 function 而不注入它,或者将 function 包装在另一个您可以标记为可注入的服务中(比如说,一个 uuid 提供程序服务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM