Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问怎么在一个api中注入两个不同的service? #10

Open
huangyujun opened this issue Sep 22, 2020 · 3 comments
Open

请问怎么在一个api中注入两个不同的service? #10

huangyujun opened this issue Sep 22, 2020 · 3 comments

Comments

@huangyujun
Copy link

如题:
有些业务场景需要操作两个不同的表,那就会涉及到两个service,看到例子中只能在构造器里注入一个service,那另一个service怎么注入呢

@yacan8
Copy link
Owner

yacan8 commented Sep 22, 2020

如题:
有些业务场景需要操作两个不同的表,那就会涉及到两个service,看到例子中只能在构造器里注入一个service,那另一个service怎么注入呢

构造函数中可以根据需求取出对应的service,取出的同时,awilix会自动实例化对应的service,比如

@route('/todo')
export default class TodoAPI {
  constructor({ todoService, otherService }) {
    this.todoService = todoService;
    this.otherService = otherService; // 注入otherService
  }

}

@huangyujun
Copy link
Author

huangyujun commented Sep 22, 2020

如题:
有些业务场景需要操作两个不同的表,那就会涉及到两个service,看到例子中只能在构造器里注入一个service,那另一个service怎么注入呢

构造函数中可以根据需求取出对应的service,取出的同时,awilix会自动实例化对应的service,比如

@route('/todo')
export default class TodoAPI {
  constructor({ todoService, otherService }) {
    this.todoService = todoService;
    this.otherService = otherService; // 注入otherService
  }

}

这个方式试过啦,会报500 @yacan8 ,不知道是不是我命名的问题? 我的service 命名 GameDataService,在构造器中是 gameDataService 这么写吧?

@yacan8
Copy link
Owner

yacan8 commented Sep 23, 2020

如题:
有些业务场景需要操作两个不同的表,那就会涉及到两个service,看到例子中只能在构造器里注入一个service,那另一个service怎么注入呢

构造函数中可以根据需求取出对应的service,取出的同时,awilix会自动实例化对应的service,比如

@route('/todo')
export default class TodoAPI {
  constructor({ todoService, otherService }) {
    this.todoService = todoService;
    this.otherService = otherService; // 注入otherService
  }

}

这个方式试过啦,会报500 @yacan8 ,不知道是不是我命名的问题? 我的service 命名 GameDataService,在构造器中是 gameDataService 这么写吧?

500的具体报错内容是什么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants