7

I am trying to do simple redux application with angular 6.

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
            })
        }   
      }
}

in the above code I created reducers and successfully registered within the store module.Then I import the store in my service(records.service.ts) file.By subscribing into my getState() I get my current state but when I calling the updateState() to dispatch an action for updating the state it shows following error in my terminal and also update my state.

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>'.
Pranab V V
  • 1,386
  • 5
  • 27
  • 53

1 Answers1

3

I face the same issue and found a quick fix.

Instead of using store.select('appReducer').dispatch just use store.dispatch.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177