当前位置:   article > 正文

flask前后端项目--实例-前端部分:-4-vue-Element Plus_elementplus 前后台项目示例

elementplus 前后台项目示例

flask前后端项目--实例-前端部分:-4-vue-Element Plus组件添加事项

一、实验测试步骤

1.Element Plus添加

1.先备份App.VUE,然后修改app.vue的内容,数据来源资Element Plus的表格table

2. 数据来源资Element Plus的表格table

3. 运行服务,展示界面数据,说明复制的数据是正确

 


二、实际项目代码

实施前记得备份修改的数据

2.1修改前端魔板内容,展示的内容

  1. <template>
  2. <div style="margin: 0 auto;width: 100%">
  3. <h1 style="text-align: center"> 图书管理系统</h1>
  4. <!-- 添加图书按钮-->
  5. <el-button type="primary" @click="add_dialog_visible=true" size="small">添加图书</el-button>
  6. <!-- 展示数据的表格-->
  7. <el-table :data="books" style="margin: 20px auto;">
  8. <el-table-column label="编号" prop="book_number"></el-table-column>
  9. <el-table-column label="书名" prop="book_name"></el-table-column>
  10. <el-table-column label="类型" prop="book_type"></el-table-column>
  11. <el-table-column label="价格" prop="book_prize"></el-table-column>
  12. <el-table-column label="作者" prop="book_author"></el-table-column>
  13. <el-table-column label="出版社" prop="book_publisher"></el-table-column>
  14. <el-table-column label="操作" align="right" width="200px">
  15. <template #default="scope">
  16. <el-button size="small" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
  17. <el-button size="small" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
  18. </template>
  19. </el-table-column>
  20. </el-table>
  21. </div>
  22. </template>

2.2代码部分内容:

  1. <script setup>
  2. import axios from "axios";
  3. import {reactive,ref,onMounted} from "vue";
  4. import {ElMessageBox}from 'element-plus'
  5. const books=reactive([])
  6. //请求后端数据,像后端请求数据
  7. const getStudent=()=>{
  8. axios.get("http://127.0.0.1:5000/books",).then(
  9. res=>{
  10. //删除旧的数据
  11. books.splice(0,books.length)
  12. //解压新的数据并且添加
  13. books.push(...res.data.results)
  14. console.log("更新数据")
  15. }
  16. )
  17. }
  18. //在页面上删除数据
  19. const handleDelete=(index,scope)=>{
  20. console.log(index,scope)
  21. axios.delete("http://127.0.0.1:5000/books/${scope.id}").then(
  22. ()=>{getStudent()}
  23. )
  24. }
  25. //页面加载完毕后再获取后台数据,生命周期的一个钩子函数
  26. onMounted(()=>{
  27. getStudent()
  28. })
  29. </script>

三、展示界面

3.1后台启动:

3.2.前台运行

3.3界面展示--已经从后端获取了数据

 


 备注内容信息

3.4备注:---前端

 

前端APP.vue中所有代码

  1. <script setup>
  2. import axios from "axios";
  3. import {reactive,ref,onMounted} from "vue";
  4. import {ElMessageBox}from 'element-plus'
  5. const books=reactive([])
  6. //请求后端数据,像后端请求数据
  7. const getStudent=()=>{
  8. axios.get("http://127.0.0.1:5000/books",).then(
  9. res=>{
  10. //删除旧的数据
  11. books.splice(0,books.length)
  12. //解压新的数据并且添加
  13. books.push(...res.data.results)
  14. console.log("更新数据")
  15. }
  16. )
  17. }
  18. //在页面上删除数据
  19. const handleDelete=(index,scope)=>{
  20. console.log(index,scope)
  21. axios.delete("http://127.0.0.1:5000/books/${scope.id}").then(
  22. ()=>{getStudent()}
  23. )
  24. }
  25. //页面加载完毕后再获取后台数据,生命周期的一个钩子函数
  26. onMounted(()=>{
  27. getStudent()
  28. })
  29. </script>
  30. <template>
  31. <div style="margin: 0 auto;width: 100%">
  32. <h1 style="text-align: center"> 图书管理系统</h1>
  33. <!-- 添加图书按钮-->
  34. <el-button type="primary" @click="add_dialog_visible=true" size="small">添加图书</el-button>
  35. <!-- 展示数据的表格-->
  36. <el-table :data="books" style="margin: 20px auto;">
  37. <el-table-column label="编号" prop="book_number"></el-table-column>
  38. <el-table-column label="书名" prop="book_name"></el-table-column>
  39. <el-table-column label="类型" prop="book_type"></el-table-column>
  40. <el-table-column label="价格" prop="book_prize"></el-table-column>
  41. <el-table-column label="作者" prop="book_author"></el-table-column>
  42. <el-table-column label="出版社" prop="book_publisher"></el-table-column>
  43. <el-table-column label="操作" align="right" width="200px">
  44. <template #default="scope">
  45. <el-button size="small" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
  46. <el-button size="small" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
  47. </template>
  48. </el-table-column>
  49. </el-table>
  50. </div>
  51. </template>
  52. <style scoped>
  53. </style>

 man.js代码

  1. import {createApp, reactive} from 'vue'
  2. import './style.css'
  3. import App from './App.vue'
  4. import ElementPlus from 'element-plus'
  5. import 'element-plus/dist/index.css'
  6. createApp(App).use(ElementPlus).mount('#app')
  7. // createApp(App).mount('#app')

 

3.5后端所有代码--后端代码组织结构

 后端提供的接口代码:

extension.py
  1. from flask_sqlalchemy import SQLAlchemy
  2. from flask_cors import CORS
  3. db=SQLAlchemy()
  4. #跨域请求的问题
  5. cors=CORS()
  6. # 所有的其他扩展 文件都是在这里编写

 models.py
  1. from extension import db
  2. class Book(db.Model):
  3. __tablename__='book'
  4. id=db.Column(db.Integer,primary_key=True,autoincrement=True)
  5. book_number=db.Column(db.String(255),nullable=False)
  6. book_name = db.Column(db.String(255), nullable=False)
  7. book_type = db.Column(db.String(255), nullable=False)
  8. book_price = db.Column(db.Float, nullable=False)
  9. author = db.Column(db.String(255))
  10. book_publisher=db.Column(db.String(255))
  11. # 初始化数据,制作数据
  12. @staticmethod
  13. def init_db():
  14. res=[
  15. (1,'001','猪儿八级天宫','小说',100,'猪八戒','xx出版'),
  16. (2,'002','孙悟空天宫','小说',000,'孙悟空','xx出版'),
  17. ]
  18. for ret in res:
  19. book=Book()
  20. book.id=ret[0]
  21. book.book_number=ret[1]
  22. book.book_name=ret[2]
  23. book.book_type=ret[3]
  24. book.book_price=ret[4]
  25. book.author=ret[5]
  26. book.book_publisher=ret[6]
  27. db.session.add(book)
  28. db.session.commit()

app.py

  1. from flask import Flask,request
  2. from flask.views import MethodView
  3. from extension import db,cors
  4. from models import Book
  5. app = Flask(__name__)
  6. app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///books.sqlite'
  7. app.config['SQLAlCHEMY_TRACK_MODIFICATIONS']=False
  8. db.init_app(app)
  9. cors.init_app(app)
  10. # api提供类
  11. class BookApi(MethodView):
  12. def get(self,book_id):
  13. #没有传查询的参数,就是默认查询所有
  14. if not book_id:
  15. books:[Book]=Book.query.all()
  16. # 列表推导式,从books中取出数据分别赋值给book对象的不同字段,形成给book对象赋值过程
  17. results=[
  18. {
  19. 'id':book.id,
  20. 'book_name':book.book_name,
  21. 'book_type':book.book_type,
  22. 'book_price':book.book_price,
  23. 'book_number':book.book_number,
  24. 'book_publisher':book.book_publisher,
  25. 'author':book.author,
  26. }for book in books
  27. ]
  28. return {
  29. 'status':'success',
  30. 'message':'数据查询成功',
  31. 'results':results
  32. }
  33. #传查询的参数,就是按照查询的参数进行查询数据
  34. book: [Book] = Book.query.get(book_id)
  35. return {
  36. 'status': 'success',
  37. 'message': '数据查询成功',
  38. 'result':{
  39. 'id': book.id,
  40. 'book_name': book.book_name,
  41. 'book_type': book.book_type,
  42. 'book_price': book.book_price,
  43. 'book_number': book.book_number,
  44. 'book_publisher': book.book_publisher,
  45. 'author': book.author,
  46. }
  47. }
  48. def post(self):
  49. # json上传数据的过程
  50. form=request.json
  51. book=Book()
  52. book.book_number=form.get('book_number')
  53. book.book_name=form.get('book_name')
  54. book.book_type=form.get('book_type')
  55. book.book_price=form.get('book_price')
  56. book.author=form.get('author')
  57. book.book_publisher=form.get('book_publisher')
  58. db.session.add(book)
  59. db.session.commit()
  60. return {
  61. 'status':'success',
  62. 'message':'数据添加成功!'
  63. }
  64. def delete(self,book_id):
  65. book=Book.query.get(book_id)
  66. db.session.delete(book)
  67. db.session.commit()
  68. return {
  69. 'status':'success',
  70. 'message':'数据delete成功!'
  71. }
  72. def put(self,book_id):
  73. book:Book=Book.query.get(book_id)
  74. book.book_type=request.json.get('book_type')
  75. book.book_name=request.json.get('book_name')
  76. book.book_price=request.json.get('book_price')
  77. book.book_number=request.json.get('book_number')
  78. book.book_publisher=request.json.get('book_publisher')
  79. book.author=request.json.get('author')
  80. db.session.commit()
  81. return {
  82. 'status': 'success',
  83. 'message': '数据update成功!'
  84. }
  85. # api提供类的注册的连接,测试可以参考下面的url进行测试
  86. book_view=BookApi.as_view('book_api')
  87. app.add_url_rule('/books',defaults={'book_id':None},view_func=book_view,methods=['GET',])
  88. app.add_url_rule('/books',view_func=book_view,methods=['POST',])
  89. app.add_url_rule('/books/<int:book_id>',view_func=book_view,methods=['GET','PUT','DELETE'])
  90. @app.route('/')
  91. def hello_world(): # put application's code here
  92. return 'Hello World!'
  93. @app.route("/login")
  94. def login():
  95. return "login"
  96. #生成数据库文件,在程序目录下可以找bools。sqlite这个数据库
  97. #然后再终端中运行‘flask create ’这个命令,就是可以最终生成数据库了
  98. #如果提示报错,多生成几次
  99. @app.cli.command()
  100. def create():
  101. db.drop_all()
  102. db.create_all()
  103. Book.init_db()
  104. if __name__ == '__main__':
  105. app.run(debug=True)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/66537
推荐阅读
相关标签
  

闽ICP备14008679号