ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ import {createStore} from './redux'; let counterValEle = document.getElementById('counterVal'); let addBtn = document.getElementById('addBtn'); let minusBtn = document.getElementById('minusBtn'); function reducer(state={number:0},action){ switch(action.type){ case 'ADD': return {number:state.number+1}; case 'MINUS': return {number:state.number-1}; default: return state; } } let store = createStore(reducer); function render(){ counterValEle.innerHTML = store.getState().number; } render(); store.subscribe(render); addBtn.addEventListener('click',()=>store.dispatch({type:'ADD'})); minusBtn.addEventListener('click',()=>store.dispatch({type:'MINUS'})); ~~~