{"id":1614,"date":"2023-06-18T13:21:39","date_gmt":"2023-06-18T05:21:39","guid":{"rendered":"https:\/\/blog.xtaa.cn\/?p=1614"},"modified":"2023-06-18T13:21:39","modified_gmt":"2023-06-18T05:21:39","slug":"1614","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2023\/06\/18\/1614\/","title":{"rendered":"Vue\u548cFlask\u5b9e\u73b0\u524d\u540e\u7aef\u5206\u79bb"},"content":{"rendered":"\n<p>\u540e\u7aef\u91c7\u7528Flask\u63d0\u4f9b\u51e0\u4e2aRestfull\u98ce\u683c\u7684API\uff0c\u524d\u7aef\u653e\u5f03\u60ef\u7528\u7684flasky\u5e38\u7528\u7684Jinja\u6a21\u677f\u5f15\u64ce\uff0c\u91c7\u7528\u7075\u6d3b\u7684Vue.js\u6846\u67b6\uff0c\u987a\u4fbf\u5b9e\u8df5\u4e00\u628a\u524d\u540e\u7aef\u5206\u79bb\u3002\u65e2\u7136\u524d\u540e\u7aef\u5206\u79bb\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5728\u5f00\u53d1\u73af\u5883\u72ec\u7acb\u521b\u5efa\u4e24\u4e2a\u9879\u76ee\u3002\u4e0b\u9762\u9010\u4e00\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<p>\u5b89\u88c5node.js<\/p>\n\n\n\n<p>\u5b89\u88c5vue\uff0c\u5b89\u88c5webpack\uff0c\u5b89\u88c5vue-cli\uff0c\u521b\u5efa\u5de5\u7a0b<\/p>\n\n\n\n<p>vue\u662f\u4ee5\u7ec4\u4ef6\u4e3a\u5355\u4f4d\u7ec4\u7ec7\u6210\u590d\u6742\u7684\u9875\u9762\u3002\u6211\u4eec\u76f4\u63a5\u5728componets\u4e0b\u7684HelloWorld.vue\u4e0a\u8fdb\u884c\u4ee3\u7801\u8c03\u6574\u548c\u7f16\u5199\u3002template\u90e8\u5206\u4e3b\u8981\u5305\u542b\u4e00\u4e2aselect\u5143\u7d20\uff0cvalue\u5bf9\u5e94\u540e\u7aefAPI\uff0c\u89e6\u53d1button\u4f1a\u5411\u9009\u62e9\u7684\u540e\u7aefAPI\u53d1\u9001\u8bf7\u6c42\u3002script\u90e8\u5206methods\u90e8\u5206\u5b9e\u73b0\u5bf9\u5143\u7d20\u4e8b\u4ef6\u7684\u54cd\u5e94\uff0c\u901a\u8fc7axios\u4e0e\u540e\u7aef\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\uff0c\u5206\u522b\u91c7\u7528get\u548cpost\u65b9\u6cd5\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div class=\"hello\">\n    &lt;button @click=\"test\">\u6d4b\u8bd5&lt;\/button>\n    &lt;select v-model=\"selected\" name=\"url-option\">\n      &lt;option value=\"\">\u9009\u62e9\u4e00\u4e2aUrl&lt;\/option>\n      &lt;option value=\"\/api\/speech\/event_extraction\">\u601d\u5fc5\u9a70\u8b66\u60c5\u4fe1\u606f\u62bd\u53d6&lt;\/option>\n      &lt;option value=\"\/api\/speech\/addr_fix\">\u601d\u5fc5\u9a70\u5730\u5740\u7406\u89e3&lt;\/option>\n      &lt;option value=\"\/api\/gaode\/get_poi\">\u9ad8\u5fb7\u5173\u952e\u5b57\u641c\u7d22&lt;\/option>\n    &lt;\/select>\n    &lt;input v-model=\"input_query\" style=\"width: 400px\">\n    &lt;button @click=\"sendRequest\">\u53d1\u9001\u8bf7\u6c42&lt;\/button>\n    &lt;br>&lt;\/br>\n    &lt;textarea style=\"width: 600px; height: 300px\">{{ resp }}&lt;\/textarea>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport axios from 'axios'\n\/\/ \u540e\u7aef\u670d\u52a1\u5668\u5730\u5740\naxios.defaults.baseURL = 'http:\/\/10.67.*.*:5000';\nexport default {\n  name: 'HelloWorld',\n  data () {\n    return {\n      selected: '',\n      input_query: '',\n      resp: ''\n    }\n  },\n  methods: {\n    test () {\n      axios\n        .get('\/api\/hello')\n        .then(response => {\n          console.log(response.data.name)\n          this.resp = response.data.name\n        })\n        .cache(error => {\n          console.error(error)\n        })\n    },\n    sendRequest () {\n      console.log(this.selected)\n      axios\n        .post(this.selected, {query: this.input_query})\n        .then(response => {\n          console.log(response)\n          this.resp = response.data\n        }).catch(error => {\n          console.error(error)\n        })\n    }\n  }\n}\n&lt;\/script>\n\n&lt;!-- Add \"scoped\" attribute to limit CSS to this component only -->\n&lt;style scoped>\nh1, h2 {\n  font-weight: normal;\n}\nul {\n  list-style-type: none;\n  padding: 0;\n}\nli {\n  display: inline-block;\n  margin: 0 10px;\n}\na {\n  color: #42b983;\n}\n&lt;\/style>\n\n<\/code><\/pre>\n\n\n\n<p>\u5728vue\u4e2d\u8fd8\u7528<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">v-for \u6765\u5728\u524d\u7aef\u9875\u9762\u4e2d\u505afor\u5faa\u73af\uff0cv-on:click=\u70b9\u51fb\uff0cv-if=\u8868\u793aIF\u8bed\u53e5<\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u540e\u7aef<\/mark><\/strong><\/p>\n\n\n\n<p>\u6253\u5f00pycharm\uff0c\u65b0\u5efa\u4e00\u4e2aflask\u9879\u76ee<\/p>\n\n\n\n<p>\u5728app.py\u91cc\u7f16\u5199\u51e0\u4e2aRestfull \u98ce\u683c\u7684API\uff0c\u7528\u4e8e\u54cd\u5e94\u524d\u7aef\u8bf7\u6c42<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from flask import Flask, jsonify, request\nfrom flask_cors import CORS\nimport requests\nimport json\n\napp = Flask(__name__)\n# \u5b9e\u73b0\u8de8\u57df\u8bbf\u95ee\ncors = CORS(app, resources={r\"*\": {\"origins\": \"*\"}})\n\n# get\u8bf7\u6c42\n@app.route('\/api\/hello')\ndef hello_world():\n    content = {\n            \"name\": \"\u7f51\u7ad9\",\n            \"num\": 3,\n            \"sites\": &#91;{\"name\": \"Google\", \"info\": &#91;\"Android\", \"Google \u641c\u7d22\", \"Google \u7ffb\u8bd1\"]},\n                      {\"name\": \"Runoob\", \"info\": &#91;\"\u83dc\u9e1f\u6559\u7a0b\", \"\u83dc\u9e1f\u5de5\u5177\", \"\u83dc\u9e1f\u5fae\u4fe1\"]},\n                      {\"name\": \"Taobao\", \"info\": &#91;\"\u6dd8\u5b9d\", \"\u7f51\u8d2d\"]}],\n            }\n\n    return jsonify(content)\n\n# post\u8bf7\u6c42\n@app.route('\/api\/gaode\/get_poi', methods=&#91;'POST'])\ndef get_poi():\n    json_data = request.get_json()\n    url_prefix = \"***\"   \n    url = url_prefix + '&amp;keywords=' + json_data&#91;'query']\n    headers = {\"Content-Type\": \"application\/json\"}\n    resp = requests.get(url, headers=headers)\n    text = resp.json()\n    return text\n\n# \u5176\u5b83\u63a5\u53e3\u6b64\u5904\u53ef\u8fdb\u884c\u8865\u5145.............\n\nif __name__ == '__main__':\n    app.run(\n        # TODO \u8bbe\u7f6e\u65e0\u6548\uff0c\u539f\u56e0\u672a\u77e5\n        # host='10.67.*.*',\n        # port='5000',\n        # debug=True\n    )\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u540e\u7aef\u91c7\u7528Flask\u63d0\u4f9b\u51e0\u4e2aRestfull\u98ce\u683c\u7684API\uff0c\u524d\u7aef\u653e\u5f03\u60ef\u7528\u7684flasky\u5e38\u7528\u7684Jinja\u6a21\u677f\u5f15\u64ce\uff0c\u91c7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-1614","post","type-post","status-publish","format-standard","hentry","category-24"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1614"}],"version-history":[{"count":0,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1614\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1614"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}