{"id":7831,"date":"2023-11-28T20:00:27","date_gmt":"2023-11-28T12:00:27","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7831"},"modified":"2023-12-01T23:02:19","modified_gmt":"2023-12-01T15:02:19","slug":"vue-zhongaxios-shi-shen-me-zen-me-shi-yong-miao-sh","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/vue-zhongaxios-shi-shen-me-zen-me-shi-yong-miao-sh\/","title":{"rendered":"Vue \u4e2d axios \u662f\u4ec0\u4e48\uff1f\u600e\u4e48\u4f7f\u7528\uff1f\u63cf\u8ff0\u4f7f\u7528\u5b83\u5b9e\u73b0\u767b\u5f55\u529f\u80fd\u7684\u6d41\u7a0b\uff1f"},"content":{"rendered":"<p><code>axios<\/code> \u662f\u4e00\u4e2a\u57fa\u4e8e Promise \u7684 HTTP \u5ba2\u6237\u7aef\uff0c\u7528\u4e8e\u5728\u6d4f\u89c8\u5668\u548c Node.js \u4e2d\u8fdb\u884c HTTP \u8bf7\u6c42\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u3001\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u6267\u884c\u5f02\u6b65\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5728 Vue \u9879\u76ee\u4e2d\u7528\u6765\u4e0e\u540e\u7aef\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<h3><a id=\"%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8axios%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5982\u4f55\u4f7f\u7528 axios\uff1a<\/h3>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u5df2\u7ecf\u5b89\u88c5\u4e86 axios\uff1a<\/p>\n<pre><code class=\"language-bash\">npm install axios\n<\/code><\/pre>\n<p>\u5728 Vue \u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 axios \u53d1\u9001 HTTP \u8bf7\u6c42\u3002\u4f8b\u5982\uff0c\u53d1\u9001\u4e00\u4e2a GET \u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u5728 Vue \u7ec4\u4ef6\u4e2d\u4f7f\u7528 axios\nimport axios from 'axios';\n\naxios.get('https:\/\/api.example.com\/data')\n  .then(response =&gt; {\n    \/\/ \u5904\u7406\u54cd\u5e94\u6570\u636e\n    console.log(response.data);\n  })\n  .catch(error =&gt; {\n    \/\/ \u5904\u7406\u9519\u8bef\n    console.error('Error fetching data:', error);\n  });\n<\/code><\/pre>\n<h3><a id=\"%E4%BD%BF%E7%94%A8axios%E5%AE%9E%E7%8E%B0%E7%99%BB%E5%BD%95%E5%8A%9F%E8%83%BD%E7%9A%84%E6%B5%81%E7%A8%8B%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 axios \u5b9e\u73b0\u767b\u5f55\u529f\u80fd\u7684\u6d41\u7a0b\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u767b\u5f55\u8868\u5355\uff1a<\/strong> \u5728 Vue \u7ec4\u4ef6\u4e2d\u521b\u5efa\u767b\u5f55\u8868\u5355\uff0c\u5305\u62ec\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u5b57\u6bb5\u4ee5\u53ca\u63d0\u4ea4\u6309\u94ae\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u76d1\u542c\u8868\u5355\u63d0\u4ea4\u4e8b\u4ef6\uff1a<\/strong> \u76d1\u542c\u8868\u5355\u63d0\u4ea4\u4e8b\u4ef6\uff0c\u5728\u7528\u6237\u70b9\u51fb\u767b\u5f55\u6309\u94ae\u65f6\u89e6\u53d1\u767b\u5f55\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u9001\u767b\u5f55\u8bf7\u6c42\uff1a<\/strong> \u5728\u767b\u5f55\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528 axios \u53d1\u9001 POST \u8bf7\u6c42\u5230\u540e\u7aef\u767b\u5f55\u63a5\u53e3\uff0c\u4f20\u9012\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u767b\u5f55\u54cd\u5e94\uff1a<\/strong> \u6839\u636e\u540e\u7aef\u8fd4\u56de\u7684\u54cd\u5e94\uff0c\u53ef\u80fd\u662f\u6210\u529f\u767b\u5f55\u6216\u767b\u5f55\u5931\u8d25\u3002\u6210\u529f\u767b\u5f55\u65f6\u53ef\u4ee5\u4fdd\u5b58\u7528\u6237\u4fe1\u606f\u6216 token\uff0c\u5931\u8d25\u65f6\u53ef\u4ee5\u5c55\u793a\u9519\u8bef\u4fe1\u606f\u7ed9\u7528\u6237\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u793a\u4f8b\u4ee3\u7801\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-vue\">&lt;template&gt;\n  &lt;form @submit.prevent=&quot;login&quot;&gt;\n    &lt;input v-model=&quot;username&quot; type=&quot;text&quot; placeholder=&quot;Username&quot; \/&gt;\n    &lt;input v-model=&quot;password&quot; type=&quot;password&quot; placeholder=&quot;Password&quot; \/&gt;\n    &lt;button type=&quot;submit&quot;&gt;Login&lt;\/button&gt;\n  &lt;\/form&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport axios from 'axios';\n\nexport default {\n  data() {\n    return {\n      username: '',\n      password: ''\n    };\n  },\n  methods: {\n    login() {\n      axios.post('https:\/\/api.example.com\/login', {\n        username: this.username,\n        password: this.password\n      })\n      .then(response =&gt; {\n        \/\/ \u5904\u7406\u767b\u5f55\u6210\u529f\u7684\u54cd\u5e94\uff0c\u4fdd\u5b58\u7528\u6237\u4fe1\u606f\u6216 token\n        console.log('Login successful:', response.data);\n      })\n      .catch(error =&gt; {\n        \/\/ \u5904\u7406\u767b\u5f55\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u663e\u793a\u9519\u8bef\u4fe1\u606f\u7ed9\u7528\u6237\n        console.error('Login failed:', error.response.data);\n      });\n    }\n  }\n};\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u7684\u767b\u5f55\u6d41\u7a0b\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u66f4\u591a\u7684\u903b\u8f91\uff0c\u6bd4\u5982\u767b\u5f55\u6210\u529f\u540e\u7684\u8def\u7531\u8df3\u8f6c\u3001\u9519\u8bef\u5904\u7406\u3001\u72b6\u6001\u7ba1\u7406\u7b49\u3002\u4f46\u57fa\u672c\u7684\u6d41\u7a0b\u662f\u901a\u8fc7 axios \u53d1\u9001\u767b\u5f55\u8bf7\u6c42\uff0c\u5e76\u6839\u636e\u540e\u7aef\u8fd4\u56de\u7684\u7ed3\u679c\u5904\u7406\u76f8\u5e94\u7684\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>axios \u662f\u4e00\u4e2a\u57fa\u4e8e Promise \u7684 HTTP \u5ba2\u6237\u7aef\uff0c\u7528\u4e8e\u5728\u6d4f\u89c8\u5668\u548c Node.js \u4e2d\u8fdb\u884c HTTP [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-7831","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7831"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7831\/revisions"}],"predecessor-version":[{"id":13964,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7831\/revisions\/13964"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7831"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}