{"id":100,"date":"2022-01-13T09:05:17","date_gmt":"2022-01-13T09:05:17","guid":{"rendered":"http:\/\/114.132.224.28\/?p=100"},"modified":"2022-01-13T09:05:17","modified_gmt":"2022-01-13T09:05:17","slug":"vuex%e4%b8%ad%e9%97%b4%e4%bb%b6redux%e4%b8%ad%e9%97%b4%e4%bb%b6%e5%92%8ckoa%e4%b8%ad%e9%97%b4%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/localhost\/2022\/01\/13\/vuex%e4%b8%ad%e9%97%b4%e4%bb%b6redux%e4%b8%ad%e9%97%b4%e4%bb%b6%e5%92%8ckoa%e4%b8%ad%e9%97%b4%e4%bb%b6\/","title":{"rendered":"vuex\u4e2d\u95f4\u4ef6redux\u4e2d\u95f4\u4ef6\u548ckoa\u4e2d\u95f4\u4ef6"},"content":{"rendered":"\n
\u901a\u8fc7store.subscribe\u6ce8\u5165\u5230vuex\u4e2d\uff0c\u901a\u8fc7_subscriber\u6570\u7ec4\u6536\u96c6\u8d77\u6765\uff0c\u5728\u5185\u7f6e\u51fd\u6570commit\u7684\u65f6\u5019\u5c31\u904d\u5386\u6267\u884c\u90a3\u4e9b\u6570\u7ec4\u91cc\u9762\u7684\u51fd\u6570<\/p>\n\n\n\n
\u901a\u8fc7\u4e0d\u65ad\u5305\u88c5store.dispatch\uff0c\u6765\u5b9e\u73b0\u7ec4\u5408\u8c03\u7528\u3002\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u65e7\u7684dispatch\uff0c\u5185\u90e8\u51fd\u6570\u63a5\u53d7action\uff0c\u6267\u884c\u5b8c\u526f\u4f5c\u7528\u540e\u5728\u8c03\u7528\u65e7\u7684dispatch(action)\u3002<\/p>\n\n\n\n
\u901a\u8fc7\u6570\u7ec4\u7684reduce\uff0c\u4e0d\u65ad\u5305\u88c5\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7\u4f20\u9012dispatch\u6765\u5b8c\u6210\u7ec4\u5408\u3002\u6700\u540e\u7ec4\u6210\u7684\u65b0\u7684store.dispatch\u4f1a\u66ff\u4ee3\u539f\u672c\u7684store.dispatch,\u6570\u7ec4\u7b2c\u4e00\u4e2a\u7684store.dispatch\u4f1a\u5148\u88ab\u6267\u884c<\/p>\n\n\n\n
function logDispatch(dispatch) {\n return function(action) {\n console.log('dispatch')\n dispatch(action)\n }\n}\nfunction errorDispatch(dispatch) {\n return function(action) {\n try{\n dispatch(action)\n }catch(e){\n console.log(e)\n }\n }\n}\nfunction customDispatch(dispatch) {\n return function(action) {\n console.log('custom')\n dispatch(action)\n }\n}\nconst wrapperDisapatch = [logDispatch, errorDispatch, customDispatch].reduce(\n (wrapperDispatch,dispatch) => {\n return (...args) => wrapperDispatch(dispatch(...args))\n})\nstore.dispatch = wrapperDisapatch(store.dispatch)\nstore.dispacth({\n type: '',\n payload: ''\n})<\/code><\/pre>\n\n\n\nkoa\u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n
\u901a\u8fc7\u904d\u5386\u63d2\u4ef6\uff0c\u63a7\u5236index\u7684\u4f4d\u7f6e\u6765\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u8c03\u7528next\u3002\u5c31\u80fd\u8df3\u8f6c\u5230\u4e0b\u4e00\u4e2a\u63d2\u4ef6\u7684\u4f4d\u7f6e\uff0c\u5b98\u65b9\u7684next\u6ca1\u6709\u63a5\u6536\u53c2\u6570\uff0c\u5176\u5b9e\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u3002<\/p>\n\n\n\n
<\/p>\n\n\n\n
\u4e0b\u9762\u5229\u7528\u4e0a\u9762\u90a3\u79cd\u4e2d\u95f4\u4ef6\u6765\u5b9e\u73b0\u529f\u80fd\uff1a<\/h2>\n\n\n\n
vuex\u53ea\u662f\u5355\u7eaf\u7684\u904d\u5386\u8fd8\u662fnext\u4e0enext\u4e4b\u95f4\u6ca1\u6709\u9012\u8fdb\u5173\u7cfb\uff0c\u6ca1\u6709\u529e\u6cd5\u4e2d\u9014\u7ec8\u6b62dispatch<\/p>\n\n\n\n