氣勢(shì)的信心
計(jì)算機(jī)/健身愛(ài)好者
10 人贊了該文章
最近工作中用到Vue.js加上一直在用的Flask,所以準(zhǔn)備寫個(gè)小小的demo鞏固下。
ps:之前一直用unpkg的各種包,發(fā)現(xiàn)會(huì)出問(wèn)題,所以demo中將所有需要用到的js包放在本地當(dāng)中。
項(xiàng)目使用到Flask+flask_jsglue+vue.js+element-ui+data-table.js+axios.js
demo很簡(jiǎn)單,最基本的flask應(yīng)用加上幾個(gè)api方法
index.py:
jsglue.init_app(app) # 讓js文件中可以使用url_for方法 return render_template('index.html') return jsonify({'results': results}) @app.route('/add', methods=['POST']) name = request.json.get('name') results.append({'name': name, 'index': str(uuid1())})#uuid讓index不唯一,實(shí)際開(kāi)發(fā)中可以通過(guò)數(shù)據(jù)庫(kù)的id來(lái)代替 return jsonify({'message': '添加成功!'}), 200 @app.route('/update', methods=['PUT']) name = request.json.get('name') index = request.json.get('index') if item['index'] == index: return jsonify({'message': '編輯成功!'}), 200 @app.route('/delete', methods=['DELETE']) name = request.args.get('name') index = request.args.get('index') results.remove({'name': name, 'index': index}) return jsonify({'message': '刪除成功!'}), 200 if __name__ == '__main__':
template
<script type="text/x-template" id="tabel-detail-template"> <data-tables :data='tableData' :actions-def="getActionsDef()" :pagination-def="getPaginationDef()" :row-action-def="getRowActionsDef()" <el-table-column label="關(guān)鍵詞" min-width="400"> <a :href="scope.row.url" target="_blank" v-text="scope.row.name"></a> <el-dialog :title="formTitle" :visible.sync="dialogFormVisible"> <el-form :model="form" @submit.native.prevent> <el-form-item label="數(shù)據(jù)"> <el-input ref="valueInput" v-model.trim="form.name" auto-complete="off" @keyup.enter.native="createOrUpdate"></el-input> <div slot="footer" class="dialog-footer"> <el-button @click="dialogFormVisible = false">取 消</el-button> <el-button type="primary" @click="createOrUpdate">確 定</el-button>
index.js
getActionsDef: function () { self.formType = 'create'; self.formTitle = '添加數(shù)據(jù)'; self.dialogFormVisible = true; getPaginationDef: function () { getRowActionsDef: function () { self.form.name = row.name; self.form.index = row.index; self.formTitle = '編輯數(shù)據(jù)'; self.dialogFormVisible = true; if (row.flag === 'true') { self.$message.success("該信息不能刪除!") self.$confirm('確認(rèn)刪除該數(shù)據(jù)?', '提示', { let url = Flask.url_for("delete", {name: row.name, index: row.index}); axios.delete(url).then(function (response) { self.$message.success("刪除成功!") getCategories: function () { let url = Flask.url_for("get_base_data"); axios.get(url).then(function (response) { self.tableData = response.data.results; createOrUpdate: function () { if (self.form.name === '') { self.$message.error('數(shù)據(jù)不能為空!'); if (self.formType === 'create') { let url = Flask.url_for("add"); }).then(function (response) { self.dialogFormVisible = false; self.$message.success('添加成功!') }).catch(self.showError); let url = Flask.url_for("update", {}); }).then(function (response) { self.dialogFormVisible = false; self.$message.success('修改成功!') }).catch(self.showError);
因?yàn)閐emo比較簡(jiǎn)單,數(shù)據(jù)存在內(nèi)存中,僅僅用于展示,讀者可自行結(jié)合數(shù)據(jù)庫(kù)
代碼請(qǐng)戳github
發(fā)布于 2017-09-18
|