标签: tsyringe

Jest 测试 - 请在入口点的顶部添加“import “reflect-metadata””

我正在开发一个使用依赖注入的应用程序tsyringe。这是接收存储库作为依赖项的服务的示例:

import { injectable, inject } from 'tsyringe'

import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository'

@injectable()
export default class ListAuthorsService {
  constructor (
    @inject('AuthorsRepository')
    private authorsRepository: IAuthorsRepository
  ) {}
Run Code Online (Sandbox Code Playgroud)

和依赖项容器:

import { container } from 'tsyringe'

import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository'
import AuthorsRepository from '@domains/authors/infra/typeorm/repositories/AuthorsRepository'

container.registerSingleton<IAuthorsRepository>(
  'AuthorsRepository',
  AuthorsRepository
)

export default container
Run Code Online (Sandbox Code Playgroud)

在测试中,我不想使用在容器上注册的依赖项,而是通过参数传递模拟实例。

let authorsRepository: AuthorsRepositoryMock
let listAuthorsService: ListAuthorsService

describe('List Authors', () => {
  beforeEach(() => {
    authorsRepository = new AuthorsRepositoryMock()
    listAuthorsService = new ListAuthorsService(authorsRepository)
  })
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

tsyringe 需要反射 polyfill。请在入口点的顶部添加“import “reflect-metadata””。

我的想法是 …

node.js typescript jestjs tsyringe

5
推荐指数
2
解决办法
1565
查看次数

使用 tsyringe 进行依赖注入,与 TypeScript 进行反应

我目前的 React TypeScript 项目遇到了问题。

我用创建了我的项目npx create-react-app my-app --template typescript

我最近添加了 tsyringe 用于依赖注入,并尝试为 apiService 实现它。在按照自述文件(https://github.com/microsoft/tsyringe#injecting-primitive-values-named-injection)添加原始值之后,我遇到了障碍。我已经将experimentalDecoratorsemitDecoratorMetadata添加到我的tsconfig.json文件中,但没有成功。

我遇到的错误实际错误是:

./src/ts/utils/NetworkService.ts 9:14
Module parse failed: Unexpected character '@' (9:14)
File was processed with these loaders:
 * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| 
| let NetworkService = (_dec = singleton(), _dec(_class = (_temp = class NetworkService {
>   constructor(@inject('SpecialString')
|   value) {
|     this.str = …
Run Code Online (Sandbox Code Playgroud)

rest web typescript reactjs tsyringe

5
推荐指数
1
解决办法
3505
查看次数

使用 tsyringe 的 Typescript 动态依赖注入

我正在尝试构建和示例以了解 DI 框架/库是如何工作的,但我遇到了一些问题。

我有这个接口有两种可能的实现:

export interface Operation {
    calculate(a: number, b: number): number;
}
Run Code Online (Sandbox Code Playgroud)

子.ts

import { Operation } from "./operation.interface";

export class Sub implements Operation {
    calculate(a: number, b: number): number {
        return Math.abs(a - b);
    }
}
Run Code Online (Sandbox Code Playgroud)

总和

import { Operation } from "./operation.interface";

export class Sum implements Operation {
    calculate(a: number, b: number): number {
        return a + b;
    }

}
Run Code Online (Sandbox Code Playgroud)

计算器.ts

import { Operation } from "./operation.interface";
import {injectable, inject} from "tsyringe";

@injectable()
export class Calculator …
Run Code Online (Sandbox Code Playgroud)

dependency-injection node.js typescript tsyringe

4
推荐指数
1
解决办法
2155
查看次数

如何在 Tsyringe 中实现单例

我在实现单例时遇到问题,因为标记为 @singleton() 的类正在每个resolve() 上重新创建。

这是例子

// Foo.ts This is singleton and and must be created only once
import { injectable, singleton } from "tsyringe";

@injectable()
@singleton()
export class Foo {
  constructor() {
    console.log("Constractor of Foo");
  }
}
Run Code Online (Sandbox Code Playgroud)
// Bar.ts this is Bar and should be created every time. It uses the singleton
import { inject, injectable, Lifecycle, scoped } from "tsyringe";
import { Foo } from "./Foo";

@injectable()
export class Bar {
  constructor(@inject("Foo") private foo: Foo) {
    console.log("Constractor of Bar"); …
Run Code Online (Sandbox Code Playgroud)

singleton tsyringe

4
推荐指数
1
解决办法
4230
查看次数

Express/tsyringe TypeError:无法读取未定义的属性(读取“服务”)

我正在为学校做一个小项目,我是表达和依赖注入的新手。我开始这个小项目只是为了了解它是如何工作的。我花了几个小时试图理解这个错误,但我没有找到任何相关内容。感谢您的帮助 !

TypeError: Cannot read properties of undefined (reading 'service')
    at controllerMethod (D:\Projects\Typescript\third\dist\app.controller.js:20:14)
    at Layer.handle [as handle_request] (D:\Projects\Typescript\third\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Projects\Typescript\third\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (D:\Projects\Typescript\third\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (D:\Projects\Typescript\third\node_modules\express\lib\router\layer.js:95:5)
    at D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:346:12)
    at next (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:280:10)
    at Function.handle (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:175:3)
    at router (D:\Projects\Typescript\third\node_modules\express\lib\router\index.js:47:12) 
Run Code Online (Sandbox Code Playgroud)

这是代码:

//app.ts
import express, { json, urlencoded } from 'express'
import { APPROUTE } from './app.route'

const app = express()

app.use(json())
app.use(urlencoded({ extended: false }))
app.use(APPROUTE)

app.listen(5000, ()=>{
    console.log(`connected on port 5000`)
})


//app.controller.ts
import { Request, …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express typescript tsyringe

0
推荐指数
1
解决办法
1319
查看次数