1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to use other Angular2 service inside an ngrx/Store reducer?

Discussão em 'Angular' iniciado por Hugh Hou, Junho 8, 2025.

  1. Hugh Hou

    Hugh Hou Guest

    New to both ngrx/Store and reducer. Basically, I have this reducer:

    import {StoreData, INITIAL_STORE_DATA} from "../store-data";
    import {Action} from "@ngrx/store";
    import {
    USER_THREADS_LOADED_ACTION, UserThreadsLoadedAction, SEND_NEW_MESSAGE_ACTION,
    SendNewMessageAction
    } from "../actions";
    import * as _ from "lodash";
    import {Message} from "../../shared-vh/model/message";
    import {ThreadsService} from "../../shared-vh/services/threads.service";

    export function storeData(state: StoreData = INITIAL_STORE_DATA, action: Action): StoreData {


    switch (action.type) {

    case SEND_NEW_MESSAGE_ACTION:
    return handleSendNewMessageAction(state, action);

    default:
    return state
    }
    }

    function handleSendNewMessageAction(state:StoreData, action:SendNewMessageAction): StoreData {

    const newStoreData = _.cloneDeep(state);

    const currentThread = newStoreData.threads[action.payload.threadId];

    const newMessage: Message = {
    text: action.payload.text,
    threadId: action.payload.threadId,
    timestamp: new Date().getTime(),
    participantId: action.payload.participantId,
    id: [need a function from this service: ThreadsService]
    }

    currentThread.messageIds.push(newMessage.id);

    newStoreData.messages[newMessage.id] = newMessage;

    return newStoreData;
    }


    The problem is within the reducer function, I do not know how to inject an injectable service I created in a different file and use the function within it. The id part - I need to generate a firebase push ID using function like this.threadService.generateID() ...

    But since this is a function, I do not have a constructor to use DI and I have no idea how to get functions within threadService!

    Continue reading...

Compartilhe esta Página