{"id":1933,"date":"2022-07-30T13:14:03","date_gmt":"2022-07-30T05:14:03","guid":{"rendered":"https:\/\/yukihiew.com\/?p=1933"},"modified":"2022-08-10T23:57:36","modified_gmt":"2022-08-10T15:57:36","slug":"vue-axios-api-manage","status":"publish","type":"post","link":"https:\/\/yukihiew.com\/vue-axios-api-manage\/","title":{"rendered":"Vue | \u4f7f\u7528Axios\u505a\u6e05\u695a\u6613\u7dad\u8b77\u7684api\u7ba1\u7406"},"content":{"rendered":"\n
\u5728\u64b0\u5bebVue\u5c08\u6848\u6642\uff0c\u901a\u5e38\u90fd\u8981\u900f\u904e\u5f8c\u7aefcall api\u62ff\u8cc7\u6599\u4f86\u5b58\u5728data\u88e1\uff0c\u4e26\u4e14\u56e0\u70ba\u8cc7\u6599\u6703\u4e00\u76f4\u8b8a\u52d5\uff0c\u4e0d\u53ef\u80fd\u5beb\u6b7b\u5728\u524d\u7aef~
\u7576\u9801\u9762\u4e00\u591a\uff0c\u9700\u8981call\u7684api\u8b8a\u591a\u6642\uff0capi\u8def\u5f91\u6709\u4fee\u6539\uff0c\u5c31\u9700\u8981\u773e\u88e1\u5c0b\u5b83\u5343\u767e\u5ea6\uff0c\u4e00\u500b\u4e00\u500b\u505a\u4fee\u6b63\uff0c
\u6216\u8005\u662f\u6709\u6642\u5728\u5be9\u8996\u54ea\u4e9bapi\u6709\u7121\u4f7f\u7528\u7b49\u7b49\uff0c\u8981\u662f\u80fd\u96c6\u4e2d\u7ba1\u7406\u8207\u6aa2\u8996api\u8def\u5f91\u771f\u662f\u518d\u597d\u4e0d\u904e\u4e86\u5c0d\u5427?
\u90a3\u9ebc\u505a\u9069\u7576\u7684api\u7ba1\u7406\u662f\u83dc\u9ce5\u524d\u7aef\u7684\u4e00\u5927\u8ab2\u984c\uff0c\u4eca\u5929\u4f86\u8a18\u9304\u5728Vue\u5c08\u6848\u4e0a\u4f7f\u7528axios\u7684\u7ba1\u7406\u65b9\u5f0f\u3002<\/p>\n\n\n
axios\u6709\u63d0\u4f9b\u5e7e\u500b\u7ba1\u7406api\u7528\u7684\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n\n\n
\u7528\u4f5c\u7d71\u4e00\u7ba1\u7406api\u7684\u9032\u5165\u9ede\uff0c\u5728\u5404\u6a94\u6848\u5f15\u5165 api.js \u5f8c\u5c31\u53ef\u4ee5\u7528\u5b9a\u7fa9\u597d\u7684func\u547c\u53eb\u6574\u7406\u597d\u7684 api \u65b9\u6cd5<\/p>\n\n\n\n
import axios from 'axios';<\/code><\/pre>\n\n\n\n- axios.create() \u5efa\u7acb\u4e00\u500baxios\u5be6\u9ad4\uff0c\u50cf userRequest \u662f\u5c08\u4f9b\u4f7f\u7528\u8005\u76f8\u95dcapi\u7684axios\u5be6\u9ad4\uff0c\u53ef\u4ee5\u5275\u7acb\u5225\u7684axios\u5be6\u9ad4\u505a\u5c0d\u61c9\u76ee\u7684\u7684url\u8a2d\u5b9a\u3002<\/li><\/ul>\n\n\n\n
\/\/\u9019\u662f\u81ea\u5b9a\u7fa9\u5275\u5efa\u7684axios\u5be6\u4f8b\u7684\u9ed8\u8a8d\u503c\nconst userRequest = axios.create(\n { baseURL: 'https:\/\/api\/search\/' }\n)<\/code><\/pre>\n\n\n\n- \u5229\u7528\u525b\u525b\u5b9a\u7fa9\u7684userResearch\u518d\u6b21\u5b9a\u7fa9\u8207\u6b64api\u76f8\u95dc\u7684\u547c\u53eb (get\u3001post\u3001put\u7b49\u7b49)\uff0c\u4e14\u8981 export \u51fa\u4f86\uff0c\u5225\u7684js import\u5f8c\u624d\u80fd\u547c\u53eb\u5b83\u5594~
(\u9019\u908a\u4e0d\u7406\u89e3\u7684\u8a71\u53ef\u4ee5\u5148\u53bb\u770bjs\u7684export\u77e5\u8b58<\/a>)<\/li><\/ul>\n\n\n\nexport const apiUserLogin = data =>{\n userRequest.post( '\/signIn' , data );\n}<\/code><\/pre>\n\n\n\n\/\/\u4e5f\u53ef\u4ee5\u5b9a\u7fa9\u5b8c\u5168\u90e8\u5f8c\uff0c\u518d\u4e00\u6b21\u6027 export\nconst apiUserLogout = ( ) => {\n userRequest.get( '\/signOut' );\n};\nconst apiUserLogin = data => {\n userRequest.post( '\/signIn' , data );\n};\nexport { apiUserLogout, apiUserLogin }; <\/code><\/pre>\n\n\n2.\u4f7f\u7528 api.js \u88e1\u7684api<\/h3>\n\n\n
\u5c07\u6703\u5728\u9019\u500b\u9801\u9762\u7528\u4e0a\u7684 api \u5f9e api.js \u88e1 import \u9032\u4f86\uff0c\u518d\u52a0\u4e0a\u5f8c\u7e8c\u8981\u547c\u53eb\u7684 api \u53c3\u6578\uff0c\u5c31\u5927\u529f\u544a\u6210\uff0c\u6bd4\u8d77\u843d\u843d\u9577\u7684\u5b8c\u6574\u5beb\u51faapi\uff0c\u9019\u7a2e\u96c6\u4e2d\u7ba1\u7406\u7684\u5beb\u6cd5\u80fd\u8b93\u8981\u547c\u53eb\u7684\u53c3\u6578\u90fd\u6e05\u695a\u5448\u73fe\uff0c\u4e5f\u5f88\u597d\u5c0b\u627e\u8981\u4fee\u6539\u7684\u5c0d\u61c9api\u3002<\/p>\n\n\n\n
import { apiUserLogout, apiUserLogin } from \"api.js\";\n\n\/\/post\napiUserLogin({\n email: \"milkcat@gmail.com\",\n password: \"123456789\"\n})\n.then( res => {\n console.log( res );\n})\n.catch( err => {\n console.log(err);\n});\n\n\/\/get\napiUserLogout( )\n.then( res => {\n console.log( res.data );\n})\n.catch( err => {\n console.log(err);\n});<\/code><\/pre>\n\n\n\u5168\u5c40\u8a2d\u7f6e\u7684 axios\u9ed8\u8a8d\u503c<\/h2>\n\n\n
axios\u6709\u63d0\u4f9b\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u5982\u679c\u5927\u90e8\u5206\u7684\u57fa\u672c\u7684\u6578\u503c\u90fd\u4e00\u6a23\u3001\u8b8a\u52d5\u4e0d\u5927\uff0c\u5f8c\u7e8c\u65b0\u589e\u4e0a\u6bd4\u8f03\u65b9\u4fbf\u3002<\/p>\n\n\n\n
axios.defaults.baseURL = 'https:\/\/api.example.com';\naxios.defaults.headers.common['Authorization'] = AUTH_TOKEN;\naxios.defaults.headers.post['Content-Type'] = 'application\/x-www-form-urlencoded';<\/code><\/pre>\n\n\n\u914d\u7f6e\u512a\u5148\u9806\u5e8f<\/h2>\n\n\n
axios\u5728\u8a2d\u5b9a\u4e0a\u6709\u512a\u5148\u9806\u5e8f\uff0c\u5206\u5225\u662f: \u500b\u5225\u547c\u53eb\u6642\u8a2d\u5b9a>\u5275\u5efa\u5be6\u4f8b\u6642\u8a2d\u5b9a\u7684\u9ed8\u8a8d\u503c>\u5168\u5c40\u8a2d\u5b9a\u7684\u9ed8\u8a8d\u503c<\/p>\n\n\n\n
\u96d6\u7136\u5728\u65b0\u4eba\u6aa2\u6838\u6709\u88ab\u554f\u5230\u95dc\u65bc\u96c6\u4e2dapi\u7ba1\u7406\u7684\u5fc5\u9808\u6027\uff0c\u4e0a\u53f8\u7684\u89c0\u9ede\u662f\uff0c\u6709\u53ef\u80fd\u500b\u5225\u7684baseUrl\u9700\u8981\u4fee\u6539\u6216\u662f\u9019\u4e9bapi\u57fa\u672c\u4e0a\u4e0d\u53ef\u80fd\u88ab\u4fee\u6539\u7b49\u7b49\uff0c\u5c31\u770b\u81ea\u5df1\u7684\u5c08\u6848\u6027\u8cea\u5982\u4f55\u518d\u6c7a\u5b9a\u662f\u5426\u5c07api\u96c6\u4e2d\u7ba1\u7406\u56c9!<\/p>\n\n\n
\u53c3\u8003\u6587\u7ae0<\/h2>\n\n\n
https:\/\/medium.com\/i-am-mike\/%E4%BD%BF%E7%94%A8axios%E6%99%82%E4%BD%A0%E7%9A%84api%E9%83%BD%E6%80%8E%E9%BA%BC%E7%AE%A1%E7%90%86-557d88365619<\/a>
http:\/\/axios-js.com\/zh-cn\/docs\/index.html#axios-url-config<\/a>
https:\/\/developer.mozilla.org\/zh-TW\/docs\/Web\/JavaScript\/Reference\/Statements\/export<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u64b0\u5bebVue\u5c08\u6848\u6642\uff0c\u901a\u5e38\u90fd\u8981\u900f\u904e\u5f8c\u7aefcall api\u62ff\u8cc7\u6599\u4f86\u5b58\u5728data\u88e1\uff0c\u4e26\u4e14\u56e0\u70ba\u8cc7\u6599\u6703\u4e00\u76f4\u8b8a\u52d5\uff0c\u4e0d\u53ef\u80fd\u5beb\u6b7b […]<\/p>\n","protected":false},"author":1,"featured_media":1942,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[124],"tags":[123],"class_list":["post-1933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-124","tag-vue","entry","has-media"],"yoast_head":"\n
Vue | \u4f7f\u7528Axios\u505a\u6e05\u695a\u6613\u7dad\u8b77\u7684api\u7ba1\u7406 · \u9019\u88e1\u662fYUKI<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\t\n\t\n\t\n