angular6 - 在 Angular 6 中使用 redux 时,类型 'Observable<any>' 问题上不存在属性调度

标签 angular6 angular-redux

我正在尝试使用 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/

相关文章:

html - 以 Angular 6 动态创建 html 导航标签

javascript - 如何在.net core 2.1和Angular 6中使用外部Js文件

javascript - 从 Angular-Redux 商店获取最新状态

angular - NGRX Redux 存储——仅选择状态的一部分返回 'undefined',即使智能感知自动完成并且模型中的所有内容都匹配

Angular6 Service Worker 和后台同步

Angular 6应用程序刷新后重定向到root

ngrx - first() 运算符是否会自动取消订阅?

Angular 4 Redux 选择问题(基本)

angular - 在 Angular(typescript) 中使用 tassign 的 Redux 试图加入 state.array 和 action.array 但获取 state.array.join 不是一个函数?

angular - 您正在公开分享您的 App Secret。不要在浏览器中暴露 App Secret : error while calling getStream API from Angular