{"id":445,"date":"2026-01-28T20:38:00","date_gmt":"2026-01-28T12:38:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=445"},"modified":"2026-06-16T20:55:37","modified_gmt":"2026-06-16T12:55:37","slug":"umi-dva-%e5%86%85%e7%bd%ae%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90%e7%9a%84%e6%8f%92%e4%bb%b6%e7%8a%b6%e6%80%81%ef%bc%88dva-loading%ef%bc%89","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/01\/28\/umi-dva-%e5%86%85%e7%bd%ae%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90%e7%9a%84%e6%8f%92%e4%bb%b6%e7%8a%b6%e6%80%81%ef%bc%88dva-loading%ef%bc%89\/","title":{"rendered":"Umi + Dva \u5185\u7f6e\u81ea\u52a8\u751f\u6210\u7684\u63d2\u4ef6\u72b6\u6001\uff08dva-loading\uff09"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u53ea\u8981\u4f60\u5728 <code>.umirc.ts<\/code> \u5f00\u542f\u4e86 <code>dva: {}<\/code>\uff0cUmi \u4f1a\u81ea\u52a8\u5185\u7f6e <code>dva-loading<\/code> \u63d2\u4ef6\uff0c<strong>\u4e0d\u9700\u8981\u624b\u52a8\u65b0\u5efa <code>loading.js<\/code> model<\/strong>\uff1b<\/li>\n\n\n\n<li>\u63d2\u4ef6\u5185\u90e8\u81ea\u52a8\u6ce8\u5165\u4e00\u5957 reducer\u3001\u76d1\u542c\u6240\u6709 <code>effects<\/code> \u5f02\u6b65\u51fd\u6570\uff0c\u81ea\u52a8\u7ef4\u62a4\u5168\u5c40 <code>loading<\/code> \u6570\u636e\uff1b<\/li>\n\n\n\n<li>\u5168\u5c40\u6839 <code>state<\/code> \u9876\u5c42\u76f4\u63a5\u591a\u51fa\u4e00\u4e2a <code>loading<\/code> \u5bf9\u8c61\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u5199 <code>state => state.loading<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e95\u5c42\u539f\u7406\uff08\u63d2\u4ef6\u81ea\u52a8\u52ab\u6301 effect\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d2\u4ef6\u5229\u7528 dva \u7684 <code>onEffect<\/code> \u94a9\u5b50\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4efb\u610f <code>yield call()<\/code> \u53d1\u8d77\u63a5\u53e3\u8bf7\u6c42\u524d \u2192 \u81ea\u52a8 dispatch action\uff0c\u6807\u8bb0 loading = true\uff1b<\/li>\n\n\n\n<li>\u63a5\u53e3\u8bf7\u6c42\u6210\u529f \/ \u5931\u8d25\u7ed3\u675f\u540e \u2192 \u81ea\u52a8 dispatch action\uff0c\u6807\u8bb0 loading = false\uff1b \u5168\u7a0b\u4e0d\u7528\u4f60\u624b\u52a8\u5199 <code>setLoading(true\/false)<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001loading \u5bf9\u8c61\u5b8c\u6574\u7ed3\u6784\uff08\u6253\u5370\u770b\u5f97\u5230\uff09<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>loading = {\n  global: false,          \/\/ \u5168\u5c40\uff1a\u4efb\u610f\u63a5\u53e3\u5728\u8bf7\u6c42\u5c31\u4e3a true\n  models: {\n    user: false,          \/\/ user \u6a21\u5757\u4efb\u610feffect\u8bf7\u6c42\u4e2d=true\n    goods: false\n  },\n  effects: {\n    'user\/fetchInfo': false, \/\/ \u7cbe\u51c6\u5355\u4e2a\u63a5\u53e3 loading\n    'goods\/getList': false\n  }\n}\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u6bcf\u4e2a\u5b57\u6bb5\u4f5c\u7528 &amp; \u4e1a\u52a1\u4f7f\u7528\u573a\u666f<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. <code>loading.global<\/code>\uff08\u5168\u5c40\u8f6c\u5708\u3001\u5168\u5c40\u906e\u7f69\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u8981\u9879\u76ee\u91cc<strong>\u4efb\u610f\u63a5\u53e3\u6b63\u5728\u8bf7\u6c42<\/strong>\uff0c\u503c\u4e3a <code>true<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const loading = useSelector(state => state.loading);\n\/\/ \u5168\u5c40loading\u906e\u7f69\n&lt;Spin spinning={loading.global}>{\u9875\u9762\u5185\u5bb9}&lt;\/Spin><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. <code>loading.models.xxx<\/code>\uff08\u6309\u6a21\u5757\u63a7\u5236 loading\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">xxx \u662f model \u7684 namespace\uff0c\u8be5 model \u4e0b\u4efb\u610f effect \u8bf7\u6c42\u4e2d = true\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ user\u6a21\u578b\u4efb\u610f\u63a5\u53e3\u52a0\u8f7d\u65f6\uff0c\u8868\u683c\u8f6c\u5708\nconst userLoading = useSelector(state => state.loading.models.user);\n&lt;Table loading={userLoading} \/><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u7cbe\u51c6\u5355\u4e2a\u63a5\u53e3 loading<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>loading.effects&#91;'namespace\/effect\u540d']<\/code>\uff08\u7cbe\u51c6\u5355\u4e2a\u63a5\u53e3 loading\uff09<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u76d1\u542c\u67d0\u4e00\u4e2a\u5177\u4f53\u63a5\u53e3\uff0c\u9002\u5408\u540c\u4e00\u9875\u9762\u591a\u4e2a\u6309\u94ae\u5206\u522b loading\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u53ea\u76d1\u542c user\/fetchList \u8fd9\u4e2aeffect\nconst tableLoading = useSelector(state =&gt; state.loading.effects&#91;'user\/fetchList']);\nconst submitLoading = useSelector(state =&gt; state.loading.effects&#91;'user\/save']);\n\n&lt;Table loading={tableLoading} \/&gt;\n&lt;Button loading={submitLoading}&gt;\u63d0\u4ea4&lt;\/Button&gt;\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5168\u5c40\u7edf\u4e00\u52a0\u8f7d\u52a8\u753b\u3001\u9875\u9762\u906e\u7f69\uff1b<\/li>\n\n\n\n<li>\u63a7\u5236 Table\u3001Modal\u3001Form \u7684\u52a0\u8f7d\u8f6c\u5708\uff1b<\/li>\n\n\n\n<li>\u9632\u6b62\u91cd\u590d\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\uff08loading \u4e3a true \u65f6\u7981\u7528\u6309\u94ae\uff09\uff1b<\/li>\n\n\n\n<li>\u9aa8\u67b6\u5c4f\u3001\u52a0\u8f7d\u63d0\u793a\u6587\u6848\u7edf\u4e00\u7ba1\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b8c\u6574\u7ec4\u4ef6\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useSelector, useDispatch } from 'dva';\nimport { Spin, Table, Button } from 'antd';\n\nexport default function UserPage() {\n  const dispatch = useDispatch();\n  \/\/ \u83b7\u53d6\u6574\u5957loading\u72b6\u6001\n  const loading = useSelector(state =&gt; state.loading);\n  \/\/ \u7cbe\u51c6\u53d6\u5f53\u524d\u8868\u683c\u63a5\u53e3loading\n  const tableLoading = loading.effects&#91;'user\/fetchList'];\n\n  const loadData = () =&gt; {\n    dispatch({ type: 'user\/fetchList' });\n  };\n\n  return (\n    &lt;Spin spinning={loading.global}&gt;\n      &lt;Button onClick={loadData} loading={tableLoading}&gt;\n        \u5237\u65b0\u5217\u8868\n      &lt;\/Button&gt;\n      &lt;Table loading={tableLoading} \/&gt;\n    &lt;\/Spin&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u5e38\u89c1\u7591\u95ee<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4e3a\u4ec0\u4e48\u6211\u9879\u76ee\u91cc\u627e\u4e0d\u5230 <code>loading.js<\/code> model\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u662f\u6846\u67b6<strong>\u5185\u7f6e\u63d2\u4ef6\u81ea\u52a8\u751f\u6210<\/strong>\uff0c\u4e0d\u662f\u624b\u52a8\u521b\u5efa\u7684 src\/models \u6587\u4ef6\uff0c\u6e90\u7801\u5728 umi \u5185\u90e8\u63d2\u4ef6\u91cc\uff0c\u9879\u76ee\u76ee\u5f55\u770b\u4e0d\u5230\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5173\u95ed dva \u540e loading \u5c31\u6d88\u5931<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>.umirc.ts<\/code> \u5982\u679c\u5220\u6389 <code>dva:{immer:true}<\/code>\uff0cdva \u63d2\u4ef6\u5931\u6548\uff0c<code>state.loading<\/code> \u4f1a\u76f4\u63a5\u4e0d\u5b58\u5728\uff0c\u8bbf\u95ee\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4e3a\u4ec0\u4e48\u4e0d\u7528\u81ea\u5df1\u7ef4\u62a4 loading \u53d8\u91cf\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f20\u7edf redux \u9700\u8981\u6bcf\u4e2a effect \u624b\u52a8\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4f20\u7edf\u5199\u6cd5\uff0c\u5197\u4f59\nyield put({type:'user\/setLoading', payload:true})\nyield call(request, '\/api')\nyield put({type:'user\/setLoading', payload:false})\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>dva-loading<\/code> \u5168\u81ea\u52a8\u5904\u7406\uff0c\u7701\u53bb\u91cd\u590d\u4ee3\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e95\u5c42\u539f\u7406\uff08\u63d2\u4ef6\u81ea\u52a8\u52ab\u6301 effect\uff09 \u63d2\u4ef6\u5229\u7528 ..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,12,11],"tags":[],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-js","category-react-","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/react-%e5%89%8d%e7%ab%af\/\" rel=\"category tag\">React<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}