{"id":385,"date":"2022-03-23T20:08:22","date_gmt":"2022-03-23T12:08:22","guid":{"rendered":"http:\/\/gjweb.top\/?p=385"},"modified":"2022-03-23T20:08:22","modified_gmt":"2022-03-23T12:08:22","slug":"%e4%bd%bf%e7%94%a8vue3%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%e5%9c%a8%e7%bb%84%e4%bb%b6%e4%b8%ad%e6%97%a0%e6%b3%95%e8%8e%b7%e5%8f%96%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=385","title":{"rendered":"\u4f7f\u7528Vue3\u5168\u5c40\u53d8\u91cf\u5728\u7ec4\u4ef6\u4e2d\u65e0\u6cd5\u83b7\u53d6\u53d8\u91cf"},"content":{"rendered":"\n<p>\u5728Vue3\u5b98\u65b9\u6587\u6863\u4e2d\u8bf4 \u4f7f\u7528 <strong>globalProperties<\/strong> \u548c <strong>getCurrentInstance<\/strong> \u5728\u5168\u5c40\u4e2d\u6ce8\u518c\u548c\u4f7f\u7528\u53d8\u91cf\uff0c\u5176\u8bed\u6cd5\u7b49\u540c Vue2 \u4e2d\u7684 <strong>Vue.prototype.xxx<\/strong><\/p>\n\n\n\n<p>\u5728 main.js \u4e2d\u6ce8\u518c\u5168\u5c40\u53d8\u91cf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { createApp } from 'vue'<br>import axios from \"axios\"<br>const app = createApp(App)<br>app.config.globalProperties.$axios = axios<\/code><\/pre>\n\n\n\n<p>\u5728\u5176\u4ed6\u7ec4\u4ef6\u4f7f\u7528 axios, <strong>\u6ce8\u610f\u6709\u4e9b\u6587\u6863\u4e2d\u4f1a\u5c06 proxy \u5199\u6210 ctx \uff0c\u4f46\u662f\u4eb2\u6d4b ctx \u4e2d\u65e0\u6cd5\u53d6\u5f97\u5168\u5c40\u53d8\u91cf*<\/strong> \uff0c\u6b64\u65f6\u7684 proxy \u76f8\u5f53\u4e8e this.$axios<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { getCurrentInstance } from \"vue\"<br>export default {<br> &nbsp; &nbsp;name:\"component\",<br> &nbsp; &nbsp;setup(){<br> &nbsp; &nbsp; &nbsp; &nbsp;const {proxy} = getCurrentInstance()<br> &nbsp; &nbsp; &nbsp; &nbsp;const gitData = async ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let response = await proxy.$axios({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;method:\"get\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url:\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  })<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(proxy)<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Vue3\u5b98\u65b9\u6587\u6863\u4e2d\u8bf4 \u4f7f\u7528 globalProperties \u548c getCurrentInstance \u5728\u5168 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-385","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=385"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/385\/revisions"}],"predecessor-version":[{"id":386,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/385\/revisions\/386"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}