如何以及在哪里将项目放入本地存储库中

时间:2019-02-07 14:25:01

标签: angular ionic-framework redux ionic2 ionic3

我正在使用离子4和角钢。也是插件-ng2-redux。

我调度了一个事件。我在商店里抓到它。我将捕获的项目存储在存储中,但我还需要将该项目存储在localStorage中。如您所知,由于我正在开发离子应用程序,因此我没有window.localStorage。因此,我需要包括存储模块,还需要一个构造函数来注入它,但是我在商店中没有构造函数。人们还说不要在减速器中将放置物品写到本地存储中。那我该怎么办?

import { SET_USER_ROLE } from "./actions";
import { Storage } from '@ionic/storage'
export interface IUsersState{
    access_token : string,
    refresh_token :string, 
    role_name: string,
    data: Object
}

export const USERS_INITIAL_STATE: IUsersState = {
    access_token: null,
    refresh_token: null,
    role_name: null,
    data : null
}

export function usersReducer(state: IUsersState = USERS_INITIAL_STATE, action) : IUsersState{
    switch(action.type){
        case SET_USER_ROLE =>{
            // I want to put role_name in localstorage but to do that, i have to write 
            // this.storage.set (but storage needs to be injected in constructor, but i don't have one)
            // also it's said that it's bad to put it in localstorage at this place reducer.
            return {...state, role_name: action.role_name};
        }
    }
    return state;
}

1 个答案:

答案 0 :(得分:0)

您可以使用intel security插件。