I use standalone components in Angular 17. When I used module architecture, I didn't have this problem. I added it to import in AppModule
and it works good.
imports: [
TranslateModule.forRoot(loader)
],
But if I add TranslateModule.forRoot(loader)
in standalone components
@Component({
selector: 'app-main-work-space',
standalone: true,
imports: [
// @ts-ignore
TranslateModule.forRoot(loader)
],
templateUrl: './main-work-space.component.html',
styleUrl: './main-work-space.component.scss'
})
In result I have this mistake.
✘ [ERROR] TS-992012: 'imports' contains a ModuleWithProviders value, likely the result of a 'Module.forRoot()'-style call. These calls are not used to configure components and are not valid in standalone component imports - consider importing them in the application bootstrap instead.
I try add @ts-ignore
but it doesn't help.
How can I fix it?
NgModule
)? – MgSomeModule.forRoot()
once in your application (in theimports
of a rootNgModule
when calling.bootstrapModule()
inmain.ts
). Some packages have aSomeModule.forChild()
method that can be used to import the package in a childNgModule
. – Mg.bootstrapApplication()
and itsApplicationConfiguration
in which you can specify providers as if there were a rootNgModule
. There are cleaner ways if the package supports them, but callingSomeModule.forRoot()
here is supported. – MgApplicationConfig
– Quail.importProvidersFrom()
like so:.bootstrapApplication(AppComponent, { providers: [ importProvidersFrom(TranslateModule.forRoot(loader)) ] });
. – Mg/main-work-space.component.html
.translate
pipe is red and it proposes addTranslateModule
to import inMainWorkSpaceComponent
– Quailimports: [TranslateModule]
in your component. There's no need to call.forRoot()
there. This is the same thing you would do if importing into anAppModule
(where you call.forRoot()
) and aSharedModule
(where you don't call.forRoot()
). – Mgmain.ts:5 ERROR NullInjectorError: R3InjectorError(Standalone[_AppComponent])[_TranslateService -> _TranslateService -> _TranslateService]:
– Quail.forRoot()
when bootstrapping the application. IfTranslateModule.forRoot()
doesn't return a provider forTranslateService
, you might have to add one on your own:providers: [TranslateModule.forRoot(loader), { provide: TranslateService, useFactory/useClass/useExisting: ... }]
. That's falling into configuration of@ngx-translate
, though. – Mg