我正在尝试使用 angular 6 做简单的 redux 应用程序。
减速器.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;
}
动作.ts
export const login="LOGIN";
export const logout="LOGOUT"
索引.ts
import { reducer,appReducer } from './reducer';
import { ActionReducerMap } from '@ngrx/store';
interface appState {
appReducer:appReducer;
}
export const reducers:ActionReducerMap<appState>= {
appReducer:reducer
}
记录服务.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 并成功在 store 模块中注册。然后我将 store 导入到我的 service(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>'.