1

I'm using @Inject(String) to enable giving config at the instantiation of my service.

Here's the code:

const httpLoaderFactory: (http: HttpClient) => TranslateHttpLoader = (http: HttpClient) =>
    new TranslateHttpLoader(http, "./i18n/", ".json");

export const appConfig: ApplicationConfig = {
    providers: [
        provideHttpClient(),
        provideTranslateService({
            loader: {
                provide: TranslateLoader,
                useFactory: httpLoaderFactory,
                deps: [HttpClient],
            },
        }),
    ],
};

And this is the constructor of my TranslateHTTPLoader class:

constructor(
    private http: HttpClient,
    @Inject(String) public prefix = "/assets/i18n/",
    @Inject(String) public suffix = ".json",
) {}

Angular 20 ESLint now wants me to write it as follows:

http = inject(HttpClient)

But this won't take the element from the constructor. Is it a way of doing it properly?

I'm already disabling the ESLint errors using:

// eslint-disable-next-line @angular-eslint/prefer-inject
private http: HttpClient,

2 Answers 2

2

first: @Inject(String) makes almost zero sense. it means you are trying to inject something provided with a token String and that is most likely not provided, or if someone did, then it is most likely something stupid.

second: you can make use of DI with the help of inject and some other parameters that are not provided in the DI as constructor params and have these both things on your class simultaniously

private http = inject(HttpClient);
constructor(
    public prefix = "/assets/i18n/", // not sure why you want defaults if you are passing them in the factory anyway
    public suffix = ".json",
) {}


export const appConfig: ApplicationConfig = {
    providers: [
        provideTranslateService({
            loader: {
                provide: TranslateLoader,
 // factory is called inside of an injection context. that means that "inject" will work
                useFactory: () => new TranslateHttpLoader("./i18n/", ".json"),
            },
        }),
    ],
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thx for the response, but either way if I remove the @Inject(string), which somehow I though to be obligated, I still get the following error : Prefer using the inject() function over constructor parameter injection. Do you understand the reason why ?
hmm, not sure. did you take http =... line from the constructor to be just a class field definition? just normal constructor parameters are supposed to be same as the last example in their "valid code examples". github.com/angular-eslint/angular-eslint/blob/main/packages/…
Ok, your's approach seems to be correct, but the actual state of angular-esLint make that this isn't... : github.com/angular-eslint/angular-eslint/issues/2539. Does this work on your codebase, do you have the latest angular version installed ?
0

I feel you forgot add the provideHttpClient

export const appConfig: ApplicationConfig = {
    providers: [
        provideHttpClient(), //<--add this
        provideTranslateService({
            loader: {
                provide: TranslateLoader,
                useFactory: () => new TranslateHttpLoader("./i18n/", ".json"),
            },
        }),
    ],
};

1 Comment

Hey, thx for your answer, but I did not, I just stripped the code to only show the important information, but I'll add it back in case it makes the question less understandable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.