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
TranslateService used as an injected const does not work in a const method (like HttpInterceptorFn) #44
Comments
@Laurensvdw looks like that problem exists for all functions. |
This is happening because the service parser checks only inside class declarations. I did an attempt to extend it and see if it would have been easy to parse function expressions but while it looked that it worked it fell apart in some edge cases. For example, from the code below it extracted the export const errorInterceptor: HttpInterceptorFn = (req, next) => {
const translateService = inject(TranslateService);
const router = inject(Router);
translateService.instant('translation.key').join(', ');
}; I will give it another try when I find some more time to check it out. |
This is now published as a pre-release |
I would gladly test this but the client's apps are still on Angular 16, being 8.x of this package 😭 Quite an important detail I forgot to mention... So move along, we'll update probably during the summer. 😨 As LTS is 8 Nov. 2024 for ng16 |
A fix was done for readonly inject earlier in version 9.1.1. Additionally somewhat related support is required for cases like ResolveFn, HttpInterceptorFn,... which are const's rather than classes.
In ng14 or something the following syntax
@Injectable() export class ErrorToastInterceptor implements HttpInterceptor {
was (optionally) changed to
export const errorToastInterceptor: HttpInterceptorFn = (req: HttpRequest<unknown>, next: HttpHandlerFn) => {
Where the TranslationService is injected as a const, like:
const translateService = inject(TranslateService);
translateService.instant('KEY_NAME')
is unfortunately not picked up :( I guess a method has to be added to the ast-helpers.ts to find the const translateService injectable, in a const method.Thanks for looking into it!
The text was updated successfully, but these errors were encountered: