{"id":342609,"date":"2021-02-11T21:40:35","date_gmt":"2021-02-11T13:40:35","guid":{"rendered":"http:\/\/4563.org\/?p=342609"},"modified":"2021-02-11T21:40:35","modified_gmt":"2021-02-11T13:40:35","slug":"vue-%e6%9d%83%e9%89%b4%e9%97%ae%e9%a2%98%ef%bc%8c%e5%8a%a0%e8%bd%bd%e8%b7%af%e7%94%b1%e6%97%b6%e6%9d%83%e9%99%90%e6%a8%a1%e5%9d%97%e8%bf%98%e6%b2%a1%e5%8a%a0%e8%bd%bd%e6%80%8e%e4%b9%88%e7%a0%b4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=342609","title":{"rendered":"Vue \u6743\u9274\u95ee\u9898\uff0c\u52a0\u8f7d\u8def\u7531\u65f6\u6743\u9650\u6a21\u5757\u8fd8\u6ca1\u52a0\u8f7d\u600e\u4e48\u7834\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Vue \u6743\u9274\u95ee\u9898\uff0c\u52a0\u8f7d\u8def\u7531\u65f6\u6743\u9650\u6a21\u5757\u8fd8\u6ca1\u52a0\u8f7d\u600e\u4e48\u7834\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982\u9898\uff0c\u6211\u662f\u83dc\u9e21\u524d\u7aef\uff0c<\/p>\n<p>\u6743\u9274 token \u50a8\u5b58\u5728 localStorage \u91cc\uff0c\u6bcf\u6b21\u52a0\u8f7d App.vue \u65f6\u5019\u4f1a\u8dd1\u4e00\u6bb5 js \u4ee3\u7801\u5c06\u5b83\u4ece localStorage \u62ff\u51fa\u6765\uff0c\u5b58\u5230\u4e00\u4e2a vuex \u5171\u4eab\u72b6\u6001\u91cc\u3002<\/p>\n<p>\u7136\u540e vue-router \u7684\u90e8\u5206\u5199\u4e86\u4e00\u4e2a\u62e6\u622a\u5668\uff0c\u5728\u5f00\u59cb\u6bcf\u6b21\u8bf7\u6c42\u4e4b\u524d\uff0c\u5224\u65ad vuex \u7684\u6743\u9650\u72b6\u6001\uff0c\u975e\u8ba4\u8bc1\u7684\u8bdd\u6709\u4e00\u4e9b\u8def\u7531\u4e0d\u7ed9\u52a0\u8f7d\u3002<\/p>\n<p>\u601d\u8def\u662f\u8fd9\u4e48\u60f3\u7684\uff0c\u4f46\u662f\u5b9e\u9645\u64cd\u4f5c\u7684\u65f6\u5019\u9047\u5230\u95ee\u9898\u662f vue-router \u6253\u5f00\u4e00\u4e2a\u7f51\u9875\uff0c\u5f7c\u65f6 vuex \u4ece\u672c\u5730\u5b58\u50a8\u91cc\u52a0\u8f7d token \u7684\u4ee3\u7801\u8fd8\u672a\u6267\u884c\uff0c\u6240\u4ee5\u8ba4\u4e3a\u662f\u975e\u767b\u5f55\u72b6\u6001\uff0c\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u8df3\u8f6c\u56de\u767b\u5f55\u9875\u9762\u3002<\/p>\n<p>\u60f3\u95ee\u4e00\u4e0b\u524d\u7aef\u5927\u4f6c\u8fd9\u79cd\u901a\u5e38\u89e3\u51b3\u65b9\u6848\u662f\u4ec0\u4e48<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5278231\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lzdyes <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u600e\u4e48\u4f1a\u672a\u6267\u884c\u5462\uff0c\u4f60\u521d\u59cb\u5316 state \u7684\u65f6\u5019\u5c31\u53bb\u53d6\u6301\u4e45\u5316\u7684 token \u4e0d\u5c31\u884c\u4e86<\/p>\n<p>const state = {<br \/> token: sessionStorage.getItem(&#8216;token&#8217;) || &#8221;<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5278232\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abc11 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9274\u6743\u4e0d\u662f\u6743\u9274                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5278233\" 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>                                                             \u4f60\u6700\u8fd1\u95ee\u7684\u95ee\u9898\u597d\u591a\u3002\u3002\u3002<\/p>\n<p>vue \u5f88\u4e45\u6ca1\u5199\u4e86\uff0c\u6211\u7ed9\u4f60\u4e00\u4e2a angular \u7684\u601d\u8def\uff1a<br \/>\u9996\u5148\u5efa\u7acb\u4e00\u4e2a access \u7684 svr\uff0c\u5728 app.module \u52a0\u5165\u4f9d\u8d56\uff0c\u5728 accessSvr \u91cc\u9762\u521d\u59cb\u5316 token \u6216\u8005\u7528\u6237\u6570\u636e\u3002\u7136\u540e\u5728 router guard \u91cc\u53bb\u83b7\u53d6\u5728 accessSvr \u7684\u6570\u636e\uff0c\u7528\u6765\u5224\u65ad\u767b\u5f55\u72b6\u6001\u548c\u6743\u9650\u4e4b\u7c7b\u7684\uff0c\u968f\u4f60\u53d1\u6325\uff0c\u91cd\u70b9\u662f router guard \u800c\u5df2\u3002<\/p>\n<p>vue \u7684\u8bdd\u539f\u7406\u5927\u6982\u4e00\u81f4\uff0c\u5148\u5efa\u7acb\u4e00\u4e2a access.module \u5b9e\u73b0\u7528\u6237\u6570\u636e\u4e4b\u7c7b\u7684\u64cd\u4f5c\uff0c\u7136\u540e\u5728 vue-router \u7684 guard \u53bb\u5224\u65ad\u8fdb\u5165\u8def\u7531\u7684\u65f6\u5019\u9a8c\u8bc1\u6570\u636e\uff0c\u7b26\u5408\u5c31 next()\uff0c\u4e0d\u7b26\u5408\u5c31 next(&#8216;\/login&#8217;)\u5373\u53ef<\/p>\n<p>\u90a3\u91cc\u8bf4\u7684\u4e0d\u5bf9\u671b\u6307\u6559\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5278234\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wunonglin \u5bf9\u7684\uff0c\u662f\u6211\u4e0d\u7406\u89e3 js \u6a21\u5757\u5316\u539f\u7406\uff0c\u6839\u636e\u4e0a\u8bf4\u7684\u89e3\u51b3\u4e86\uff0c\u662f\u6267\u884c\u987a\u5e8f\u7406\u89e3\u4e0d\u5230\u4f4d\u9020\u6210\u7684\uff0c\u786e\u5b9e\u53ef\u4ee5\u5728 main \u91cc\u5148\u52a0\u8f7d\u597d\u6743\u9650\u518d\u53bb route \u8fdb\u884c\u5224\u65ad                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5278235\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lzdyes \u5927\u4f6c\uff0c\u7167\u4f60\u8bf4\u7684\u65b9\u6cd5\u6743\u9274\u5df2\u7ecf\u89e3\u51b3\u4e86\uff0c\u6211\u6709\u4e2a\u95ee\u9898\u662f\uff0c\u4f60\u53d1\u7684\u4ee3\u7801\u91cc token: sessionStorage.getItem(&#8216;token&#8217;) || &#8221;\u7684\u8fd9\u4e2a\u4ece\u672c\u5730\u5b58\u50a8\u4e2d\u63d0\u53d6\u7684\u90e8\u5206\uff0c\u6211\u81ea\u5df1\u628a\u63d0\u53d6\u5c01\u88c5\u4e86\u4e00\u4e2a\u5de5\u5177\uff0c\u653e\u5728..\/plugins\/utils.js \uff0c\u56e0\u4e3a\u91cc\u9762\u4e0d\u5149\u662f\u53d6\u51fa\uff0c\u8fd8\u8981\u505a\u4e00\u4e9b parse \u4e4b\u7c7b\u7684\uff0c\u5927\u6982\u5c0f\u51e0\u5341\u884c\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u7ec4\u4ef6\u91cc\u4f7f\u7528\u7684\u65f6\u5019\u5c31\u662f this.$utils.getItem(&#8216;&#8230;&#8217;)\u8fd9\u4e48\u5199\u5c31\u884c\u4e86\uff0c\u4f46\u662f\u5728 vuex \u7684 index.js \u91cc\u5219\u8981\u6362\u6210 Vue.prototype.$utils.getItem()\u8fd9\u79cd\u3002\u6211\u5728 mutations \u91cc\u4f7f\u7528\u8fd9\u4e2a\u5199\u6cd5\u662f\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528\u7684\uff0c\u4f46\u6211\u53d1\u73b0\u5728 data \u90e8\u5206\u91cc\u8fd9\u4e48\u5199\u4f1a\u63d0\u793a Vue.prototype \u91cc\u6ca1\u6709$utils \u8fd9\u4e2a\u4e1c\u897f\u3002\u8fd9\u4e2a\u90e8\u5206\u7684\u8bdd\u5982\u679c\u60f3\u6a21\u5757\u5316\u7528\u5e94\u8be5\u600e\u4e48\u5bfc\u5165\u554a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5278236\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Carseason <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 vuex \u7684 getters \u65b9\u6cd5\u6765\u8fd4\u56de\u6570\u636e\u4e0d\u5c31\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5278237\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lzdyes <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond \u4e0d\u8981\u4e3a\u4e86\u5c01\u88c5\u800c\u5c01\u88c5\uff0cutils \u8fd9\u79cd\u5de5\u5177\u7c7b\u4e5f\u4e0d\u662f\u4e00\u5b9a\u975e\u8981\u7ed1\u5728 vue \u4e0a\u9762\uff0c\u4f60\u505a\u6210\u6a21\u5757 export \u51fa\u6765\uff0c\u7136\u540e\u5728 vuex \u91cc\u9762 import \u8fdb\u6765\u4f7f\u7528\u5373\u53ef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Vue \u6743\u9274\u95ee\u9898\uff0c\u52a0\u8f7d\u8def\u7531\u65f6\u6743\u9650\u6a21&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\/342609"}],"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=342609"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/342609\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342609"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}