Angular this.ngRedux.dispatch无法正常工作
因此,我对使用redux很陌生,一直在尝试创建一个简单的计数器应用程序,单击按钮后该应用程序会递增,但我收到以下错误消息: node_modules/@angular-redux/store/lib/src/components/ng-redux.d.ts(10,31)中出错:错误TS2420:类“NgRedux”错误地实现了接口“observestore”。 属性“dispatch”的类型不兼容。 类型“Dispatch”不可分配给类型“Dispatch”。 类型“RootState”不可分配给类型“AnyAction”。 node_modules/@angular redux/store/lib/src/components/ng redux.d.ts(37,33):错误TS2344:类型“RootState”不满足约束“Action”。 node_modules/@angular-redux/store/lib/src/components/root-store.d.ts(18,24):错误TS2344:类型“RootState”不满足约束“Action”。 src/app/app.component.ts(20,29):错误TS2345:类型为“{type:string;}”的参数不能分配给类型为“IAppState”的参数。 对象文字只能指定已知属性,类型“IAppState”中不存在“type” 这是我的app.components.ts文件 ` 这是我的app.module.ts文件:Angular this.ngRedux.dispatch无法正常工作,angular,redux,Angular,Redux,因此,我对使用redux很陌生,一直在尝试创建一个简单的计数器应用程序,单击按钮后该应用程序会递增,但我收到以下错误消息: node_modules/@angular-redux/store/lib/src/components/ng-redux.d.ts(10,31)中出错:错误TS2420:类“NgRedux”错误地实现了接口“observestore”。 属性“dispatch”的类型不兼容。 类型“Dispatch”不可分配给类型“Dispatch”。 类型“RootState”不可分配
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgRedux, NgReduxModule } from '@angular-redux/store';
import { AppComponent } from './app.component';
import { IAppState, rootReducer } from './store';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgReduxModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private ngRedux: NgRedux<IAppState>){
this.ngRedux.configureStore(rootReducer, { counter: 0 });
}
}
从'@angular/platform browser'导入{BrowserModule};
从“@angular/core”导入{NgModule};
从'@angular-redux/store'导入{NgRedux,NgReduxModule};
从“./app.component”导入{AppComponent};
从“/store”导入{IAppState,rootReducer};
@NGD模块({
声明:[
应用组件
],
进口:[
浏览器模块,
Ngredux模块
],
提供者:[],
引导:[AppComponent]
})
导出类AppModule{
构造函数(专用ngRedux:ngRedux){
this.ngRedux.configureStore(rootReducer,{counter:0});
}
}
在您的包中使用以下版本的Redux.json
,所有这些都应该可以工作:
"redux": "^3.6.0"
您可能正在使用“redux”:“^4.0.0”
。在此版本中,调度
接口的定义已更改。
从(^3.6.0)开始:
到
标记答案和以下链接的组合解决了我的所有问题:那么,什么是增量<代码>类型需要是action.ts文件中我定义的
字符串
INCREMENT。文件的外观如下:export const INCREMENT=“INCREMENT”代码>即使我将操作类型设置为“增量”,它仍然不起作用。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgRedux, NgReduxModule } from '@angular-redux/store';
import { AppComponent } from './app.component';
import { IAppState, rootReducer } from './store';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgReduxModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private ngRedux: NgRedux<IAppState>){
this.ngRedux.configureStore(rootReducer, { counter: 0 });
}
}
"redux": "^3.6.0"
export interface Dispatch<S> {
<A extends Action>(action: A): A;
}
export interface Dispatch<A extends Action = AnyAction> {
<T extends A>(action: T): T;
}
abstract dispatch: Dispatch<RootState>;
abstract dispatch: Dispatch;