在之前的項目中用到了react
因為之前也用到了全局變量劳翰,了解到了redux
但是受vuex的影響蚁署,一直想把redux在全局中使用腹侣,便把store綁到了window上
redux.js
import { createStore } from 'redux';
const initialState = {
str:"hello word"
}
const reducer = (state = initialState, action) => {
switch (action.type) {
case "test":
console.log(action)
default:
return initialState;
}
}
let store = createStore(reducer);
window.$store = store;
然后在App.js中引入redux.js
在index.js中
import "./redux/redux.js"
接下來就可以在全局中使用啦
在任意js中
window.$store.getState() //可以直接獲取到當(dāng)前的initialState的數(shù)據(jù)狀態(tài)
//執(zhí)行事件
window.$store.dispatch({type:"test",text:"我是test事件傳遞的數(shù)據(jù)"})
教程簡陋,有什么不明白的可以在評論提問
以上蒿柳。