我使用 Express 和 TypeScript 启动了一个新项目,我正在尝试使用 inversifyJS 设置 DI 容器。 它应该是非常简单的用例,有 1 个路由和控制器 到目前为止我的文件:
控制器\项目\项目.控制器
import { inject, injectable } from "inversify";
import "reflect-metadata";
import { IProjectService } from "../../services/projects/interface/IProjectService";
import { TYPES } from "../../services/projects/types";
@injectable()
class ProjectController {
private _processService: IProjectService;
constructor(@inject(TYPES.IProjectService) projectServicee: IProjectService) {
this._processService = projectServicee;
}
public getProjects(): string {
return this._processService.getProject();
}
}
export { ProjectController };
服务\项目\imp\ProjectService.ts
import { injectable } from "inversify";
import "reflect-metadata";
import { IProjectService } from "../interface/IProjectService";
@injectable()
class ProjectServicee implements IProjectService {
getProject(): string {
return "Project A";
}
}
@injectable()
class ProjectService implements IProjectService {
getProject(): string {
return "Project B";
}
}
export { ProjectServicee, ProjectService };
服务\项目\接口\IProjectService.ts
export interface IProjectService {
getProject(): string;
}
路线\projects.ts 在这里,我向容器询问 ProjectController 的实例并按预期获取它,但其中的 IProjectService 的私有实例在运行时未定义
import { myContainer } from "../../inversify.config";
import { ProjectController } from "../controllers/projects/projects.controller";
import { CONTROLLERS_TYPES } from "../controllers/types";
import * as express from "express";
const router = express.Router();
const pc = myContainer.get<ProjectController>(
CONTROLLERS_TYPES.ProjectController
);
router.get("/", pc.getProjects);
export default router;
root\inversify.config.ts
import { Container } from "inversify";
import { TYPES } from "./src/services/projects/types";
import { IProjectService } from "./src/services/projects/interface/IProjectService";
import { ProjectService } from "./src/services/projects/imp/ProjectService";
import { CONTROLLERS_TYPES } from "./src/controllers/types";
import { ProjectController } from "./src/controllers/projects/projects.controller";
const myContainer = new Container();
myContainer
.bind<ProjectController>(CONTROLLERS_TYPES.ProjectController)
.to(ProjectController);
myContainer.bind<IProjectService>(TYPES.IProjectService).to(ProjectService);
export { myContainer };
控制器类型.ts
const CONTROLLERS_TYPES = {
ProjectController: Symbol.for("ProjectController"),
};
export { CONTROLLERS_TYPES };
服务\项目 ypes.ts
const TYPES = {
IProjectService: Symbol.for("IProjectService"),
};
export { TYPES };
最后是我的主要:
import projectRoutes from "./routes/projects";
import express from "express";
const app = express();
app.get("/", function (req, res) {
res.send("Hello World");
});
app.use("/api/projects", projectRoutes);
console.log("Listening on port 3000");
app.listen(3000);
现在,当尝试调用控制器的 get 方法时,我收到错误:
Cannot read properties of undefined (reading '_processService')
这意味着控制器中 _processService 的初始化不起作用,我缺少什么?
不知道你解决了吗,我也遇到这个问题了。实际上我的代码中的所有实现都没有问题。
我认为当您在 router.get() 方法中传递 pc.getProjects 时,您会丢失“this”引用。
这就是为什么当路由器执行下面的方法时...
public getProjects(): string {
return this._processService.getProject();
}
... this._processService 未定义。
也许尝试这样做:
router.get("/", pc.getProjects.bind(pc));
而不是:
router.get("/", pc.getProjects);
这对我有用。让我知道进展如何...
我有一个与此问题类似的案例,但我无法解决它,当组件中只有构造函数时我无法注入,我的代码甚至与所提供的类似。