Angular NGXS错误:Can';t解析TranslationEditorState:(?)的所有参数
我在Angular 9应用程序中使用NGXS进行状态管理。在其中一个状态类中,任何依赖项注入都会导致错误“错误:无法解析TranslationEditorState:(?)的所有参数”。我已经尝试注入服务,甚至HttpClient,但问题仍然存在。事实上,对于任何状态类,我都会得到相同的错误。这可能与app.Module中的模块注入顺序有关吗 应用程序模块.tsAngular NGXS错误:Can';t解析TranslationEditorState:(?)的所有参数,angular,typescript,ngxs,angular9,Angular,Typescript,Ngxs,Angular9,我在Angular 9应用程序中使用NGXS进行状态管理。在其中一个状态类中,任何依赖项注入都会导致错误“错误:无法解析TranslationEditorState:(?)的所有参数”。我已经尝试注入服务,甚至HttpClient,但问题仍然存在。事实上,对于任何状态类,我都会得到相同的错误。这可能与app.Module中的模块注入顺序有关吗 应用程序模块.ts import { BrowserModule } from '@angular/platform-browser'; import {
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 { 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);
});
}
}
从'rxjs/operators'导入{tap};
从“../actions”导入{GetTranslations};
从“../model”导入{TranslationEditor};
从'@ngxs/store'导入{Action,State,StateContext};
从“../../translation editor/service/translation editor.service”导入{TranslationEditorService};
@陈述({
名称:“translationEditor”,
默认值:{
翻译:{},
},
})
导出类TranslationEditorState{
构造函数(私有translationEditorService:translationEditorService){}
@动作(GetTranslations)
getTranslations({getState,patchState}:StateContext,{application,environment,languageCodes}){
返回此.translationEditorService
.getTranslations(应用程序、环境、语言代码)
.烟斗(
点击(翻译=>{
补丁状态({
…getState(),
翻译,
});
})
)
.订阅(响应=>{
控制台日志(响应);
});
}
}
服务:
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);
}
}
从'rxjs'导入{Observable};
从'@ngxs/Store'导入{Store};
从“@angular/core”导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“../../shared/model”导入{InitialData,LabelValue};
@注射的({
providedIn:'根',
})
导出类TranslationEditorService{
baseUrl:字符串;
构造函数(私有只读http:HttpClient,私有只读存储:存储){
this.baseUrl=this.store.selectSnapshot(state=>state.appConfig.baseapirl);
}
getInitialData():可观察{
常量url=`${this.baseUrl}/初始数据`;
返回此.http.get(url)
}
getTranslations(应用程序:LabelValue,环境:LabelValue,语言代码:数组):可观察{
const url=`${this.baseUrl}/applications/translations${application.value}/env/${environment.value}/translations`;
返回this.http.post(url,语言代码);
}
}
您需要添加@Injectable for IVY