我在使用redux-thunk设置redux存储时遇到此错误。我的应用程序是非常简单的计数器应用程序。即使站点正常运行,我的代码编辑器仍显示类型错误。
类型'(dispatch:any)=> void'的参数不可分配给 类型为“ AnyAction”的参数。类型中缺少属性“类型” '((dispatch:any)=> void',但类型为'AnyAction'.ts(2345)
是必需的
是因为我以错误的方式设置了它吗?有什么办法可以使此错误消失?
这是我的app.module.ts
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { NgRedux, NgReduxModule } from "@angular-redux/store";
import { IAppState, rootReducer, INITIALSTATE } from "./store";
import thunk from "redux-thunk";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, NgReduxModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(ngRedux: NgRedux<IAppState>) {
ngRedux.configureStore(rootReducer, INITIALSTATE, [thunk]);
}
}
这是我的app.component.ts
import { Component } from "@angular/core";
import { NgRedux, select } from "@angular-redux/store";
import { IAppState } from "./store";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
@select("counter") counter;
constructor(private ngRedux: NgRedux<IAppState>) {
ngRedux.subscribe(() => {
// console.log(ngRedux.getState());
});
}
onClick() {
this.ngRedux.dispatch(this.increment());
}
increment() {
return dispatch => {
setTimeout(() => {
dispatch(this.incrementAsync());
}, 3000);
};
}
incrementAsync() {
return { type: "INCREMENT" };
}
}
这是我的商店
export interface IAppState {
counter: number;
}
export const INITIALSTATE: IAppState = {
counter: 0
};
export function rootReducer(state: IAppState, action): IAppState {
switch (action.type) {
case "INCREMENT":
return { counter: state.counter + 1 };
}
console.log(state);
return state;
}