{"id":23258,"date":"2024-03-18T11:58:31","date_gmt":"2024-03-18T03:58:31","guid":{"rendered":"https:\/\/h5app.yimenapp.cn\/h5app\/?p=23258"},"modified":"2024-03-18T11:58:31","modified_gmt":"2024-03-18T03:58:31","slug":"nuxtaxios%e5%b0%81%e8%a3%85%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/h5app.yimenapp.cn\/h5app\/2024\/appkfa\/23258\/","title":{"rendered":"nuxtaxios\u5c01\u88c5\u64cd\u4f5c\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"<p>Nuxt.js \u662f\u4e00\u4e2a\u57fa\u4e8e Vue.js \u7684\u670d\u52a1\u7aef\u6e32\u67d3\u5e94\u7528\u6846\u67b6\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5730\u642d\u5efa\u4e00\u4e2a\u9ad8\u5ea6\u53ef\u5b9a\u5236\u7684\u3001SEO \u53cb\u597d\u7684\u3001\u6e10\u8fdb\u5f0f\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u800c\u5728 Nuxt.js \u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 nuxt-axios \u8fd9\u4e2a\u63d2\u4ef6\u6765\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u7684\u5c01\u88c5\u3002<\/p>\n<p>nuxt-axios \u7684\u5b89\u88c5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5b89\u88c5 axios \u548c nuxt-axios \u4e24\u4e2a\u4f9d\u8d56\u5373\u53ef\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>npm install axios nuxt-axios<\/p>\n<p>&#8220;`<\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5728 nuxt.config.js \u4e2d\u8fdb\u884c\u914d\u7f6e\uff1a<\/p>\n<p>&#8220;`js<\/p>\n<p>module.exports = {<\/p>\n<p>  modules: [<\/p>\n<p>    &#8216;@nuxtjs\/axios&#8217;,<\/p>\n<p>  ],<\/p>\n<p>  axios: {<\/p>\n<p>    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u8fdb\u884c axios \u7684\u5168\u5c40\u914d\u7f6e<\/p>\n<p>  },<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u9879\u76ee\u4e2d\u4f7f\u7528 nuxt-axios \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u9875\u9762\u7ec4\u4ef6\u4e2d\u901a\u8fc7 this.$axios \u8bbf\u95ee axios \u5b9e\u4f8b\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`vue<\/p>\n<p>\n<p>\n<p>    <button>\u8bf7\u6c42\u6570\u636e<\/button><\/p>\n<p>    {{ data }}<\/p>\n<\/p>\n<\/p>\n<p>export default {<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      data: null,<\/p>\n<p>    }<\/p>\n<p>  },<\/p>\n<p>  methods: {<\/p>\n<p>    async fetchData() {<\/p>\n<p>      const res = await this.$axios.get(&#8216;\/api\/data&#8217;)<\/p>\n<p>      this.data = res.data<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>}<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u53d1\u9001\u4e00\u4e2a GET \u8bf7\u6c42\uff0c\u5e76\u5c06\u8fd4\u56de\u7684\u6570\u636e\u6e32\u67d3\u5230\u9875\u9762\u4e0a\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u76f4\u63a5\u5728\u9875\u9762\u7ec4\u4ef6\u4e2d\u4f7f\u7528 axios \u5e76\u4e0d\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 nuxt-axios \u63d0\u4f9b\u7684\u4e00\u4e9b\u529f\u80fd\u6765\u5bf9\u7f51\u7edc\u8bf7\u6c42\u8fdb\u884c\u5c01\u88c5\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u5171\u4eab\u8fd9\u4e9b\u5c01\u88c5\u597d\u7684\u8bf7\u6c42\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 nuxt.config.js \u4e2d\u5168\u5c40\u914d\u7f6e axios\uff1a<\/p>\n<p>&#8220;`js<\/p>\n<p>module.exports = {<\/p>\n<p>  modules: [<\/p>\n<p>    &#8216;@nuxtjs\/axios&#8217;,<\/p>\n<p>  ],<\/p>\n<p>  axios: {<\/p>\n<p>    baseURL: process.env.BASE_URL || &#8216;http:\/\/localhost:3000&#8217;,<\/p>\n<p>  },<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u91cc\u8bbe\u7f6e\u4e86 axios \u7684 baseURL \u5c5e\u6027\uff0c\u4f7f\u5f97\u6211\u4eec\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4e0d\u9700\u8981\u5199\u5b8c\u6574\u7684 URL\uff0c\u53ea\u9700\u8981\u5199\u76f8\u5bf9\u8def\u5f84\u5373\u53ef\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u5176\u4ed6 axios \u7684\u5168\u5c40\u914d\u7f6e\uff0c\u4f8b\u5982\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3001\u54cd\u5e94\u62e6\u622a\u7b49\u7b49\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a axios \u5b9e\u4f8b\uff0c\u5e76\u5c06\u5176\u5c01\u88c5\u6210\u4e00\u4e2a\u63d2\u4ef6\u3002\u5728 plugins \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a axios.js \u6587\u4ef6\uff1a<\/p>\n<p>&#8220;`js<\/p>\n<p>import axios from &#8216;axios&#8217;<\/p>\n<p>const instance = axios.create({<\/p>\n<p>  baseURL: process.env.BASE_URL || &#8216;http:\/\/localhost:3000&#8217;,<\/p>\n<p>})<\/p>\n<p>export<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/15\/12766754067.jpg\" \/><\/figure>\n<\/p>\n<p> default ({ app }, inj<a href=\"https:\/\/www.yimenapp.net\/dabao-1594.html\">ios\u7f51\u7ad9<\/a>ect) =&gt; {<\/p>\n<p>  inject(&#8216;axios&#8217;, {<\/p>\n<p>    get(url, config) {<\/p>\n<p>      return instance.get(url, config)<\/p>\n<p>    },<\/p>\n<p>    post(url, data, config) {<\/p>\n<p>      return instance.post(url, data, co<a href=\"https:\/\/www.yimenapp.net\/dabao-2253.html\">\u7f51\u7ad9\u8f6capk<\/a>nfig)<\/p>\n<p>    },<\/p>\n<p>    \/\/ \u5176\u4ed6\u8bf7\u6c42\u65b9\u6cd5\u7684\u5c01\u88c5<\/p>\n<p>  })<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u4e2a\u63d2\u4ef6\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a axios \u5b9e\u4f8b\uff0c\u5e76\u5c06\u5176\u5c01\u88c5\u6210\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u4e2d\u5305\u542b\u4e86\u6211\u4eec\u5e38\u7528\u7684\u8bf7\u6c42\u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7 inject \u5c06\u8fd9\u4e2a\u5bf9\u8c61\u6ce8\u5165\u5230 Vue \u5b9e\u4f8b\u4e2d\uff0c\u4f7f\u5f97\u6211\u4eec\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u90fd\u53ef\u4ee5\u4f7f\u7528 this.$axios \u8bbf\u95ee\u8fd9\u4e9b\u5c01\u88c5\u597d\u7684\u8bf7\u6c42\u65b9\u6cd5\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5728\u9875\u9762\u7ec4\u4ef6\u4e2d\u5c31\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u6211\u4eec\u5c01\u88c5\u597d\u7684\u8bf7\u6c42\u65b9\u6cd5\u4e86\uff1a<\/p>\n<p>&#8220;`vue<\/p>\n<p>\n<p>\n<p>    <button>\u8bf7\u6c42\u6570\u636e<\/button><\/p>\n<p>    {{ data }}<\/p>\n<\/p>\n<\/p>\n<p>export default {<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      data: null,<\/p>\n<p>    }<\/p>\n<p>  },<\/p>\n<p>  methods: {<\/p>\n<p>    async fetchData() {<\/p>\n<p>      const res = await this.$axios.get(&#8216;\/api\/data&#8217;)<\/p>\n<p>      this.data = res.data<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>}<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u5171\u4eab\u8fd9\u4e9b\u5c01\u88c5\u597d\u7684\u8bf7\u6c42\u65b9\u6cd5\u4e86\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u5168\u5c40\u914d\u7f6e\u6765\u7edf\u4e00\u5904\u7406\u4e00\u4e9b\u7f51\u7edc\u8bf7\u6c42\u7684\u7ec6\u8282\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nuxt.js \u662f\u4e00\u4e2a\u57fa\u4e8e Vue.js \u7684\u670d\u52a1\u7aef\u6e32\u67d3\u5e94\u7528\u6846\u67b6\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5730\u642d\u5efa\u4e00\u4e2a\u9ad8\u5ea6\u53ef\u5b9a\u5236\u7684\u3001SEO \u53cb\u597d\u7684\u3001\u6e10\u8fdb\u5f0f\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u800c\u5728 Nuxt.js \u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 n<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[25513,25512,427,416,25514],"topic":[],"class_list":["post-23258","post","type-post","status-publish","format-standard","hentry","category-appkfa","tag-25513","tag-25512","tag-427","tag-416","tag-app"],"_links":{"self":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/23258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/comments?post=23258"}],"version-history":[{"count":1,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/23258\/revisions"}],"predecessor-version":[{"id":23315,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/23258\/revisions\/23315"}],"wp:attachment":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/media?parent=23258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/categories?post=23258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/tags?post=23258"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/topic?post=23258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}