我正在尝试使用 Angular 6 做简单的 redux 应用程序。
reducers.ts
import { login,logout } from '../actions/actions';
export interface appReducer {
login:boolean,
user:String
}
const initialstate:appReducer={
login:false,
user:'guest'
}
export function reducer(state=initialstate,action):appReducer {
switch(action.type) {
case login:
return {...state,login:true,user:action.payload}
case logout:
return {...state,login:false,user:action.payload}
}
return state;
}
actions.ts
export const login="LOGIN";
export const logout="LOGOUT"
index.ts
import { reducer,appReducer } from './reducer';
import { ActionReducerMap } from '@ngrx/store';
interface appState {
appReducer:appReducer;
}
export const reducers:ActionReducerMap<appState>= {
appReducer:reducer
}
records.service.ts
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Store } from '@ngrx/store';
export class RecordsService {
getLogin:boolean=false;
constructor(private http:HttpClient,private store:Store<any>) {
this.recordFunc();
}
getState() {
return this.store.select('appReducer');
}
updateState(action) {
if(action.login==true) {
return this.store.select('appReducer').dispatch({
type:'LOGIN',
payload:action.uname
})
}
else {
return this.store.select('appReducer').dispatch({
type:'LOGOUT',
payload:action.uname
})
}
}
}
在上面的代码中,我创建了 reducer 并在商店模块中成功注册。然后我将商店导入到我的服务(records.service.ts)文件中。通过订阅我的 getState() 我得到了当前状态,但是当我调用 updateState() 来调度一个操作来更新状态,它在我的终端中显示以下错误,并更新我的状态。
ERROR in src/app/records.service.ts(69,44): error TS2339: Property 'dispatch' does not exist on type 'Observable<any>'.
src/app/records.service.ts(75,42): error TS2339: Property 'dispatch' does not exist on type 'Observable<any>'.
最佳答案
我遇到了同样的问题并找到了快速解决方案。
不要使用 store.select('appReducer').dispatch
,只需使用 store.dispatch
。
关于angular6 - 在 Angular 6 中使用 redux 时,类型 'Observable<any>' 问题上不存在属性调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51239720/