• VUE综合数据库编程


    VUE综合数据库编程

    1.实验要求:

    基于node express+vue cli+elementUI+mysql,在如图8.14所示的功能的基础上增加一个输入框用于输入商品的id,增加一个“删除”按钮,完成根据id删除商品的功能(删除后的结果通过查看数据表goods的最新数据来验证)。

    2.参考代码

    第一步:将helloWord.vue中代码替换为:

    1. <template>
    2. <div class="hello">
    3. <h1>{{ msg }}</h1>
    4. <form >
    5. <el-row :span="12" type="flex" justify="space-around" align="middle">
    6. <el-col > <el-input v-model="name" placeholder="名称" style="width:200px"></el-input>
    7. <el-input v-model="price" placeholder="价格" style="width:200px"></el-input>
    8. <el-button type="primary" @click="addGood">提交</el-button></el-col>
    9. </el-row>
    10. <el-row type="flex" justify="space-around" align="middle">
    11. <el-col :span="12"> <el-input v-model="id" placeholder="序号" style="width:200px"></el-input>
    12. <el-button type="danger" @click="delGood">删除</el-button></el-col>
    13. </el-row>
    14. </form>
    15. </div>
    16. </template>
    17. <script>
    18. export default {
    19. name: ' HelloWorld ',
    20. data() {
    21. return {
    22. msg: 'Welcome to Your Vue.js App',
    23. name: '',
    24. price: '',
    25. id:''
    26. }
    27. },
    28. methods: {
    29. // 添加商品信息
    30. addGood() {
    31. var name = this.name;
    32. var price = this.price;
    33. //通过http post方式访问后端服务器。http post方式用于上传数据到服务器。
    34. //http.post的第一个参数为请求地址,第二个参数为携带的参数,第三参数可以为空,但不能省略。
    35. this.$http.post('/api/good/addGood', {
    36. name: name,
    37. price: price
    38. }, {}).then((response) => {
    39. console.log(response);
    40. })
    41. },
    42. // 删除商品的信息
    43. delGood() {
    44. var id = this.id;
    45. //通过http post方式访问后端服务器。http post方式用于上传数据到服务器。
    46. //http.post的第一个参数为请求地址,第二个参数为携带的参数,第三参数可以为空,但不能省略。
    47. this.$http.post('/api/good/delGood', {
    48. id:id
    49. }, {}).then((response) => {
    50. console.log(response);
    51. })
    52. }
    53. }
    54. }
    55. </script>

    第二步:修改node中的代码。

    • sqlMap中增加: del: 'delete from goods where id=?'

    • api中增加一个删除的方法

      1. // 增加一个根据id删除商品信息的
      2. router.post('/delGood', (req, res) => {
      3. var sql = $sql.good.del;
      4. var params2 = req.body;
      5. conn.query(sql, [params2.id], function (err, result) {
      6. if (err) {
      7. console.log(err);
      8. }
      9. if (result) {
      10. console.log("result:");
      11. console.log(result);
      12. jsonWrite(res, result);
      13. }
      14. })
      15. });

    第三步:效果图如下

     

  • 相关阅读:
    Midjourney竞品Leap免费试用; Google 刚刚发布10门独立AI课程
    Python学习备忘录
    Leetcode刷题Day1----数组
    Go-知识sync map
    【注释和反射】获取class类实例的方法
    昨日,一老师课前预测日本赢球,结果令人惊叹
    抖音店铺提供优质服务|成都瀚网科技
    存储创新靠软件?
    java计算机毕业设计医院远程诊断系统源程序+mysql+系统+lw文档+远程调试
    关于 sap.ui.base.Object 的简要介绍
  • 原文地址:https://blog.csdn.net/weixin_41957626/article/details/127723439