免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
vue.js+flask+element

氣勢的信心

計算機/健身愛好者

10 人贊了該文章

最近工作中用到Vue.js加上一直在用的Flask,所以準備寫個小小的demo鞏固下。

ps:之前一直用unpkg的各種包,發(fā)現會出問題,所以demo中將所有需要用到的js包放在本地當中。

項目使用到Flask+flask_jsglue+vue.js+element-ui+data-table.js+axios.js

demo很簡單,最基本的flask應用加上幾個api方法

index.py:

  1. app = Flask(__name__)
  2. jsglue = JSGlue()
  3. jsglue.init_app(app) # 讓js文件中可以使用url_for方法
  4. @app.route('/')
  5. def index():
  6. return render_template('index.html')
  7. @app.route('/get_data')
  8. def get_base_data():
  9. return jsonify({'results': results})
  10. @app.route('/add', methods=['POST'])
  11. def add():
  12. name = request.json.get('name')
  13. results.append({'name': name, 'index': str(uuid1())})#uuid讓index不唯一,實際開發(fā)中可以通過數據庫的id來代替
  14. return jsonify({'message': '添加成功!'}), 200
  15. @app.route('/update', methods=['PUT'])
  16. def update():
  17. name = request.json.get('name')
  18. index = request.json.get('index')
  19. for item in results:
  20. if item['index'] == index:
  21. item['name'] = name
  22. break
  23. return jsonify({'message': '編輯成功!'}), 200
  24. @app.route('/delete', methods=['DELETE'])
  25. def delete():
  26. name = request.args.get('name')
  27. index = request.args.get('index')
  28. results.remove({'name': name, 'index': index})
  29. return jsonify({'message': '刪除成功!'}), 200
  30. if __name__ == '__main__':
  31. app.run(debug=True)

template

  1. <script type="text/x-template" id="tabel-detail-template">
  2. <div>
  3. <data-tables :data='tableData' :actions-def="getActionsDef()"
  4. :pagination-def="getPaginationDef()"
  5. :row-action-def="getRowActionsDef()"
  6. action-col-width="80">
  7. <el-table-column label="關鍵詞" min-width="400">
  8. <template scope="scope">
  9. <a :href="scope.row.url" target="_blank" v-text="scope.row.name"></a>
  10. </template>
  11. </el-table-column>
  12. </data-tables>
  13. <el-dialog :title="formTitle" :visible.sync="dialogFormVisible">
  14. <el-form :model="form" @submit.native.prevent>
  15. <el-form-item label="數據">
  16. <el-input ref="valueInput" v-model.trim="form.name" auto-complete="off"
  17. @keyup.enter.native="createOrUpdate"></el-input>
  18. </el-form-item>
  19. </el-form>
  20. <div slot="footer" class="dialog-footer">
  21. <el-button @click="dialogFormVisible = false">取 消</el-button>
  22. <el-button type="primary" @click="createOrUpdate">確 定</el-button>
  23. </div>
  24. </el-dialog>
  25. </div>
  26. </script>

index.js

  1. getActionsDef: function () {
  2. let self = this;
  3. return {
  4. width: 5,
  5. def: [{
  6. name: '添加數據',
  7. handler() {
  8. self.formType = 'create';
  9. self.formTitle = '添加數據';
  10. self.form.name = '';
  11. self.form.index = '';
  12. self.dialogFormVisible = true;
  13. },
  14. icon: 'plus'
  15. }]
  16. }
  17. },
  18. getPaginationDef: function () {
  19. return {
  20. pageSize: 10,
  21. pageSizes: [10, 20, 50]
  22. }
  23. },
  24. getRowActionsDef: function () {
  25. let self = this;
  26. return [{
  27. type: 'primary',
  28. handler(row) {
  29. self.formType = 'edit';
  30. self.form.name = row.name;
  31. self.form.index = row.index;
  32. self.formTitle = '編輯數據';
  33. self.dialogFormVisible = true;
  34. },
  35. name: '編輯'
  36. }, {
  37. type: 'danger',
  38. handler(row) {
  39. if (row.flag === 'true') {
  40. self.$message.success("該信息不能刪除!")
  41. } else {
  42. self.$confirm('確認刪除該數據?', '提示', {
  43. confirmButtonText: '確定',
  44. cancelButtonText: '取消',
  45. type: 'warning'
  46. }).then(function () {
  47. let url = Flask.url_for("delete", {name: row.name, index: row.index});
  48. axios.delete(url).then(function (response) {
  49. self.getCategories();
  50. self.$message.success("刪除成功!")
  51. }).catch(self.showError)
  52. });
  53. }
  54. },
  55. name: '刪除'
  56. }]
  57. },
  58. getCategories: function () {
  59. let url = Flask.url_for("get_base_data");
  60. let self = this;
  61. axios.get(url).then(function (response) {
  62. self.tableData = response.data.results;
  63. });
  64. },
  65. createOrUpdate: function () {
  66. let self = this;
  67. if (self.form.name === '') {
  68. self.$message.error('數據不能為空!');
  69. return
  70. }
  71. if (self.formType === 'create') {
  72. let url = Flask.url_for("add");
  73. axios.post(url, {
  74. name: self.form.name
  75. }).then(function (response) {
  76. self.getCategories();
  77. self.dialogFormVisible = false;
  78. self.$message.success('添加成功!')
  79. }).catch(self.showError);
  80. } else {
  81. let url = Flask.url_for("update", {});
  82. axios.put(url, {
  83. name: self.form.name,
  84. index: self.form.index
  85. }).then(function (response) {
  86. self.getCategories();
  87. self.dialogFormVisible = false;
  88. self.$message.success('修改成功!')
  89. }).catch(self.showError);
  90. }
  91. }
  92. }

因為demo比較簡單,數據存在內存中,僅僅用于展示,讀者可自行結合數據庫

代碼請戳github

發(fā)布于 2017-09-18

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
flask插件全家桶集成學習
手把手教你使用Flask搭建ES搜索引擎(預備篇)
如何用 Flask 創(chuàng)建REST API ?
flask基礎學習一
計算機技術|我的第一個Flask程序
Python Web框架Flask中使用新浪SAE云存儲實例
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服