`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./redux.min.js"></script>
</head>
<body>
<button id="add">+</button>
<span id="count">0</span>
<button id="del">-</button>
<script>
// 1. 创建 store 对象
var store = Redux.createStore(reducer)
// 2. 创建 reducer 函数, reducer返回什么, store就存储什么
var initialScale = {
count: 0
}
function reducer (state = initialScale, action) {
switch(action.type){
case 'increment':
return {
count: state.count + 1
}
case 'decrement':
return {
count: state.count - 1
}
default:
return state
}
console.log(action)
return state
}
// 3. 定义action
var increment = { type: 'increment' }
var decrement = { type: 'decrement' }
// 4. 通过dispatch 触发action
document.getElementById('add').onclick = function () {
store.dispatch(increment)
}
document.getElementById('del').onclick = function () {
store.dispatch(decrement)
}
// 5. 订阅store,改变计数器的值
store.subscribe(() => {
document.getElementById('count').innerHTML = store.getState().count
})
</script>
</body>
</html>
`