首页 > 编程知识 正文

redux 持久化,redux数据流的流程

时间:2023-05-04 10:53:44 阅读:181723 作者:4514

欢迎关注微信公众号:【 全栈攻略 】

vuex有数据持久化插件,redux有吗? 答案是肯定的。 那是一个简单易用的redux-persist,官网链接

两步走起!

一,配置我们创建store的文件

store/index.js

import { createStore,applyMiddleware,compose } from 'redux '; 导入记录来自'./reducers '; 导入来自' redux-thunk '; import { persistStore,persist reducer } from ' redux-persist '; importstoragefrom ' redux-persist/lib/storage '; //存储对象,默认为localstorageconstpersistconfig={ key : ' root ',storage,}//reducerconstpersistedreducer=}//重新组装reducers//redux调试工具配置代码constcomposeenhancers=type of window==' object ' window._ _ redux _ dev tools const enhancer=compose enhancers (apply middleware ) thunk ); 创建//storeconststore=createstore (永久记录,增强); 应用redux-persist以完成数据持久化的exportconstpersistor=persiststore (store ) export default store;二,store持久化的应用

门户文件index.js

导入来自' react '; 输入react DOM from ' react-DOM '; import { provider } from ' react-redux '; 导入存储来自'./store/index '; import { persist gate }从' redux-persist/integration/react '; import { persist or } from './store/index '; import 'antd/dist/antd.css '; import './assets/css/index.scss '; import './assets/icon/icon font.CSS '; import App from './App '; reactDOM.render(/antd国际化配置Provider store={store} //持久化store配置包含包装App根组件和所有子组件的persistgateloading={ store } 此时,我们F12看起来如下。

已成功保存到本地存储!

PS :未来的你,一定会感谢今天拼命努力的自己!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。