-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: inject class when use component by import string (#996)
- Loading branch information
Showing
8 changed files
with
93 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/core/test/fixtures/app-with-component-inject-with-class/bookstr/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "book-componentstr", | ||
"main": "src/index.ts" | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/core/test/fixtures/app-with-component-inject-with-class/bookstr/src/bookService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// src/service/bookService | ||
import { Provide, ScopeEnum } from '@midwayjs/decorator'; | ||
import { providerWrapper, IMidwayContainer } from '../../../../../src'; | ||
|
||
@Provide() | ||
export class BookServiceOne { | ||
|
||
async getAllBooks() { | ||
return [ | ||
{ | ||
"name": "无限可能str", | ||
"isbn": "9787115549440str" | ||
}, | ||
{ | ||
"name": "明智的孩子str", | ||
"isbn": "9787305236525str" | ||
}, | ||
{ | ||
"name": "伊卡狛格str", | ||
"isbn": "9787020166916str" | ||
} | ||
] | ||
} | ||
} | ||
|
||
export async function dynamicCacheServiceHandler(container: IMidwayContainer) { | ||
return () => { | ||
return 'abcstr'; | ||
} | ||
} | ||
|
||
providerWrapper([ | ||
{ | ||
id: 'dynamicCacheServicestr', | ||
provider: dynamicCacheServiceHandler, | ||
scope: ScopeEnum.Singleton, | ||
} | ||
]); |
8 changes: 8 additions & 0 deletions
8
...ages/core/test/fixtures/app-with-component-inject-with-class/bookstr/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Configuration } from '@midwayjs/decorator'; | ||
|
||
@Configuration({ | ||
namespace: 'bookstr' | ||
}) | ||
export class BookConfigurationStr { | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
packages/core/test/fixtures/app-with-component-inject-with-class/bookstr/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { BookConfigurationStr as Configuration } from './configuration'; | ||
export { BookServiceOne, dynamicCacheServiceHandler } from './bookService'; |
4 changes: 3 additions & 1 deletion
4
packages/core/test/fixtures/app-with-component-inject-with-class/main/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 9 additions & 2 deletions
11
packages/core/test/fixtures/app-with-component-inject-with-class/main/src/controller/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import { Provide, Controller, Inject, Get } from '@midwayjs/decorator'; | ||
import { BookService, dynamicCacheServiceHandler } from '../../../book'; | ||
import { BookServiceOne } from '../../../bookstr/src/bookService'; | ||
|
||
@Provide() | ||
@Controller('/user') | ||
export class UserController { | ||
@Inject() | ||
bookService: BookService; | ||
|
||
@Inject() | ||
bookServiceOne: BookServiceOne; | ||
|
||
@Inject('book:dynamicCacheService') | ||
cacheService: typeof dynamicCacheServiceHandler; | ||
|
||
@Get('/list_books') | ||
async getBooksByUser() { | ||
console.log(this.cacheService); | ||
return this.bookService.getAllBooks(); | ||
if (!this.cacheService) { | ||
throw new Error('cache service is undefined'); | ||
} | ||
const books = await this.bookServiceOne.getAllBooks(); | ||
return books.concat(await this.bookService.getAllBooks()); | ||
} | ||
} |