gpt4 book ai didi

angular - NGXS 错误 : Can't resolve all parameters for TranslationEditorState: (? )

转载 作者:行者123 更新时间:2023-12-04 12:16:48 29 4
gpt4 key购买 nike

我在 Angular 9 应用程序中使用 NGXS 进行状态管理。在其中一个状态类中,任何依赖项注入(inject)都会导致错误“错误:无法解析 TranslationEditorState 的所有参数:(?)”。我试过注入(inject)服务甚至 HttpClient,但问题仍然存在。事实上,对于任何状态类,我都会遇到相同的错误。会不会跟 app.module 中的 Module 注入(inject)顺序有关?

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { APP_INITIALIZER, NgModule } from '@angular/core';

import { NgxsModule } from '@ngxs/store';
import { AuthModule } from './auth/auth.module';
import { AppComponent } from './app.component';
import { SharedModule } from './shared/shared.module';
import { AuthInterceptor } from './auth/auth.interceptor';
import { AppRoutingModule } from './app-routing.module';
import { configFactory } from './shared/utils/config.factory';
import { NgxsFormPluginModule } from '@ngxs/form-plugin';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { ConfigService } from './shared/services/config/config.service';
import { NgxsReduxDevtoolsPluginModule } from '@ngxs/devtools-plugin';
import { TranslationEditorState } from './shared/state/translation-editor.state';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslationEditorModule } from './translation-editor/translation-editor.module';
import { ApplicationSectorState, ConfigState, InitialDataState, MenuState } from './shared/state';

const states = [ApplicationSectorState, ConfigState, InitialDataState, MenuState, TranslationEditorState];

@NgModule({
declarations: [AppComponent],
imports: [
AuthModule,
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
NgxsModule.forRoot(states),
NgxsFormPluginModule.forRoot(),
NgxsReduxDevtoolsPluginModule.forRoot(),
NgxsLoggerPluginModule.forRoot(),
SharedModule,
TranslationEditorModule,
],
providers: [
ConfigService,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: configFactory,
multi: true,
deps: [ConfigService],
},
],
bootstrap: [AppComponent],
})
export class AppModule {}

州级
import { tap } from 'rxjs/operators';
import { GetTranslations } from '../actions';
import { TranslationEditor } from '../model';
import { Action, State, StateContext } from '@ngxs/store';
import { TranslationEditorService } from '../../translation-editor/service/translation-editor.service';

@State<TranslationEditor>({
name: 'translationEditor',
defaults: {
translations: {},
},
})
export class TranslationEditorState {
constructor(private translationEditorService: TranslationEditorService) {}

@Action(GetTranslations)
getTranslations({ getState, patchState }: StateContext<TranslationEditor>, { application, environment, languageCodes }) {
return this.translationEditorService
.getTranslations(application, environment, languageCodes)
.pipe(
tap(translations => {
patchState({
...getState(),
translations,
});
})
)
.subscribe(response => {
console.log(response);
});
}
}

服务:
import { Observable } from 'rxjs';
import { Store } from '@ngxs/store';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { InitialData, LabelValue } from '../../shared/model';

@Injectable({
providedIn: 'root',
})
export class TranslationEditorService {
baseUrl: string;

constructor(private readonly http: HttpClient, private readonly store: Store) {
this.baseUrl = this.store.selectSnapshot<string>(state => state.appConfig.baseApiUrl);
}

getInitialData(): Observable<InitialData> {
const url = `${this.baseUrl}/initial-data`;
return this.http.get<InitialData>(url)
}

getTranslations(application: LabelValue, environment: LabelValue, languageCodes: Array<string>): Observable<any> {
const url = `${this.baseUrl}/applications/translations${application.value}/env/${environment.value}/translations`;
return this.http.post(url, languageCodes);
}
}

最佳答案

关于angular - NGXS 错误 : Can't resolve all parameters for TranslationEditorState: (? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60434637/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com