pra*_* vv 5 angular-redux angular6
我正在尝试使用angular 6做简单的redux应用程序。
减速器
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;
}
Run Code Online (Sandbox Code Playgroud)
动作
export const login="LOGIN";
export const logout="LOGOUT"
Run Code Online (Sandbox Code Playgroud)
索引
import { reducer,appReducer } from './reducer';
import { ActionReducerMap } from '@ngrx/store';
interface appState {
appReducer:appReducer;
}
export const reducers:ActionReducerMap<appState>= {
appReducer:reducer
}
Run Code Online (Sandbox Code Playgroud)
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
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我创建了reducer并成功在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>'.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1071 次 |
| 最近记录: |