redux.js的基本使用

1、先是安装reduxJx,  cnpm i --save rudux
2、创建一个store的js文件 
3、使用import来引用 redux     import { createStore } from 'redux'
4、然后在store 创建一个全局管理数据对象

const preloadState = {
    cartList: []
}
5、处理全局数据的方法
const reducer = function(state, action){
// 参数state:上一次的state状态
// 参数action:事件描述对象
console.log(action.type);
处理数据方法
if (item.a === 0 ){
    ...代码
}

//返回下一次全局使用的state状态
return state;
}
//创建仓库
const store = createStore(reducer, preloadState);
export default store
6、dispatch的使用 
        在store以外  store.dispatch()调用  参数1:设置store里面的action相对的type值,参数2:传递的数据  
    如下:

            store.dispatch({
                    type: 'add',
                    value: {
                        id: 2
                    }
            })
然后在store里面的action里面可以获取到 type和传入的value值,也可以在全局管理数据中的state改变全局的数据
7、在组件(页面)中也可以通过store. getState()来获取你存储的相对应的值,
8、store.subscribe()的操作是基本用来监听  store里面的一些数据变化进行操作的,需要主要的是dispatch的操作顺序,

还有一些步骤操作没有具体说,

原文地址:https://www.cnblogs.com/suichenming/p/10917284.html