{"id":360144,"date":"2021-02-15T13:56:26","date_gmt":"2021-02-15T05:56:26","guid":{"rendered":"http:\/\/4563.org\/?p=360144"},"modified":"2021-02-15T13:56:26","modified_gmt":"2021-02-15T05:56:26","slug":"vuex4-%e5%88%b0%e5%ba%95%e6%80%8e%e4%b9%88%e4%bc%98%e9%9b%85%e7%9a%84%e4%b8%8e-typescript-%e7%bb%93%e5%90%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=360144","title":{"rendered":"vuex4 \u5230\u5e95\u600e\u4e48\u4f18\u96c5\u7684\u4e0e typescript \u7ed3\u5408\u4f7f\u7528?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  vuex4 \u5230\u5e95\u600e\u4e48\u4f18\u96c5\u7684\u4e0e typescript \u7ed3\u5408\u4f7f\u7528?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : daguaochengtang <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u9996\u5148\u6392\u9664\u88c5\u9970\u5668\u5199\u6cd5\uff0c\u4e2a\u4eba\u504f\u89c1\uff0c\u5b9e\u5728\u4e0d\u559c\u6b22<\/p>\n<p>\u7136\u540e\u6211\u770b\u4e86\u4e0b\u7f51\u4e0a\u4e0d\u4e0d\u4f7f\u7528\u88c5\u9970\u5668\u7684\u5199\u6cd5\uff0c\u5f53\u7528\u5230 modules \u7684\u65f6\u5019\uff0c\u90a3\u4e2a\u7c7b\u578b\u5b9a\u4e49\uff0c\u771f\u5b9e\u6076\u5fc3\u5230\u6211\u4e86\u3002\u8d34\u4e2a\u4ee3\u7801\u7247\u6bb5\u5427\uff0c\u6bd4\u5982\u6211\u6709\u4e00\u4e2a app module\uff0c\u8fd9\u4e2a\u6a21\u5757 Store \u7684\u7c7b\u578b\u5b9a\u4e49\u5927\u81f4\u957f\u4e0b\u9762\u8fd9\u6837\uff0c\u8fd9\u73a9\u610f\u771f\u662f\u7ed9\u78b3\u57fa\u751f\u7269\u7528\u7684\u5417\uff1f\u80fd\u770b\u61c2\uff0c\u4f46\u662f\u4ee3\u7801\u771f\u5b9e\u53c8\u81ed\u53c8\u957f\u3002<\/p>\n<p>&#8220;`<br \/>export type AppStore&lt;S = AppState&gt; = Omit&lt;VuexStore&lt;S&gt;, &#8216;getters&#8217; | &#8216;commit&#8217; | &#8216;dispatch&#8217;&gt;<br \/>&amp; {<br \/> commit&lt;K extends keyof Mutations, P extends Parameters&lt;Mutations[K]&gt;[1]&gt;(<br \/> key: K,<br \/> payload: P,<br \/> options?: CommitOptions<br \/> ): ReturnType&lt;Mutations[K]&gt;<br \/>} &amp; {<br \/> dispatch&lt;K extends keyof Actions&gt;(<br \/> key: K,<br \/> payload: Parameters&lt;Actions[K]&gt;[1],<br \/> options?: DispatchOptions<br \/> ): ReturnType&lt;Actions[K]&gt;<br \/>};<br \/>&#8220;`<\/p>\n<p>\u6240\u4ee5\uff0c\u73b0\u5728 vuex4 \u7684\u73b0\u72b6\u5c31\u662f\u4e0d\u7528\u88c5\u9970\u5668\u5199 ts \u5c31\u5fc5\u987b\u5199\u8fd9\u6837\u53c8\u81ed\u53c8\u957f\u7684\u7c7b\u578b\u58f0\u660e\u5417\uff1f\u8fd8\u662f\u8bf4\u6709\u5176\u5b83\u4f18\u96c5\u4f7f\u7528\u65b9\u5f0f\uff0c\u4f46\u662f\u6211\u4e0d\u77e5\u9053\uff1f\u6b22\u8fce\u62cd\u7816      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5410447\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iikebug <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u8003\u8651\u81ea\u5df1\u5b9e\u73b0\u4e2a\u8f7b\u91cf\u7ea7\u7684\u72b6\u6001\u7ba1\u7406\u5de5\u5177                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410448\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : murmur <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u8bb0\u5f97 vuex \u975e\u5e38\u7b80\u5355\u554a\uff0c\u4e00\u4e2a\u6587\u4ef6\u5c31\u53ef\u4ee5\u641e\u5b9a\u72b6\u6001\u7ba1\u7406\uff0c\u6bd4 redux \u7b80\u5355\u591a\u4e86\uff0c\u4e3a\u5565\u8fd9\u4e48\u590d\u6742                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410449\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wunonglin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6700\u8fd1\u4e5f\u662f\u5728\u770b vue3 \u7684 ts \u6e90\u7801\uff0c\uff0c\uff0c\u7c7b\u578b\u5b9a\u4e49\u771f\u7684\u4e71\u3002\u3002\u3002\u548c ng \u4e0d\u662f\u4e00\u4e2a\u7ea7\u522b\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410450\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : daguaochengtang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @murmur typescript \u7684\u7c7b\u578b\u58f0\u660e\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410451\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : murmur <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @daguaochengtang vuex \u6700\u7b80\u5355\u7684\u65f6\u5019\u53ea\u9700\u8981\u4e00\u4e2a\u6587\u4ef6\uff0c\u91cc\u9762\u5199 state \u548c mutation\uff0c\u5176\u4f59\u7684\u90fd\u4e0d\u9700\u8981\uff0c\u4e5f\u4e0d\u8981\u6c42\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u6709\u8fd9\u4e48\u590d\u6742\u7684\u8868\u8ff0<\/p>\n<p>\u63cf\u8ff0\u590d\u6742\u4e0d\u4ee3\u8868\u8981\u5199\u8d77\u6765\u590d\u6742\u554a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410452\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : daguaochengtang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @murmur \u5e94\u8be5\u662f\u4e3a\u4e86 ts \u7684\u7c7b\u578b\u63d0\u793a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410453\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ruoxie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u4ec0\u4e48\u8fd8\u8981\u7528 vuex\uff0c\u76f4\u63a5 hooks                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410454\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : daguaochengtang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ruoxie \u770b\u8fc7\u8bf4 vue3 \u4e0d\u9700\u8981 vuex \u76f4\u63a5\u7528 hooks \u7684\uff0c\u4f46\u662f hooks \u8c8c\u4f3c\u53ea\u80fd\u7528\u5728 setup \u91cc\u5427\uff0c\u6211\u5982\u679c\u60f3\u5728\u5176\u5b83\u5730\u65b9\u7528\u5462\uff1f\u6bd4\u5982 vuex \u6211\u53ef\u4ee5\u5728 axios \u7684 request \u62e6\u622a\u5668\u91cc\u5f15\u5165 store,\u4f7f\u7528 store.user.state.token\uff0c\u4f60\u7528 hooks \u600e\u4e48\u5b9e\u73b0\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410455\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ruoxie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @daguaochengtang <\/p>\n<p>import { reactive } from &#8216;vue&#8217;;<\/p>\n<p>const user = reactive&lt;{ name: boolean,token:string, }&gt;({<br \/> name: false,<br \/> token:&#8221;<br \/>});<\/p>\n<p>export function getToken(){<br \/> return user.token<br \/>}<\/p>\n<p>export default function useUserInfo() {<br \/> &#8230;<br \/> return {<br \/> user<br \/> };<br \/>}<\/p>\n<p>\u5f53\u7136\u4f60\u53ef\u4ee5\u7c97\u66b4\u4e00\u70b9\uff0c\u76f4\u63a5\u628a user export \u51fa\u53bb                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410456\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : varzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u610f\u3002vue \u5168\u5bb6\u6876\u7528\u4e86\u4e09\u5e74\uff0c\u7ed3\u679c\u524d\u6bb5\u65f6\u95f4\u60f3\u7528 vue3 + ts \u8fd9\u5957\u4e1c\u897f\u5199\u4e2a demo \u7ec3\u624b\uff0c\u6123\u662f\u5728 vuex4 +ts \u8fd9\u5361\u7684\u65e0\u4ece\u4e0b\u624b\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5410457\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : daguaochengtang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @varzy \u771f\u662f\u6076\u5fc3\uff0c\u7a76\u5176\u539f\u56e0\u8fd8\u662f vuex \u5728\u8bbe\u8ba1\u9636\u6bb5\u5c31\u6ca1\u8003\u8651\u5230 ts \u652f\u6301\uff0cvuex4 \u4e5f\u53ea\u662f\u5728\u539f\u6765\u7684\u6e90\u7801\u57fa\u7840\u4e0a\u5411\u5916\u66b4\u9732\u4e86\u4e00\u4e9b type\uff0c\u6e90\u7801\u4f9d\u7136\u662f js \u5199\u7684\uff0c\u60f3\u6109\u5feb\u7684\u7528 ts \u5199 vuex \u4f30\u8ba1\u8981\u7b49 vuex5 \u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>vuex4 \u5230\u5e95\u600e\u4e48\u4f18\u96c5\u7684\u4e0e ty&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/360144"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360144"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/360144\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}