对数据进行一些基本操作(四)

接收post请求(vue+axios)解决跨域问题(三)

效果预览
最终效果预览
node路由配置增删改查
//查询语句
var selAll='select * from list';
//根据post的id查询
var selId='select * from list where id=?';
//插入数据
var addData='insert into list (u_name,u_phone) values (?,?)';
//删除数据
var delData='delete from list where id=?';
//更新数据
var upData='update list set u_name=?,u_phone=? where id=?'
//更新数据
router.post('/edit',function(req,res,next){
    var params=req.body;
    console.log(params)
    pool.getConnection(function(err,suc){
        suc.query(upData,[params.name,params.phone,params.id],function(err,result){
            console.log(err)
            console.log(result)
            if(result){
                result={
                    code:200,
                    msg:'更新数据成功'
                }
            }
            res.json(result);
            suc.release()
        })
    })
})
//删除数据
router.post('/del',function(req,res,next){
    var params=req.body;
    console.log(params)
    pool.getConnection(function(err,suc){
        suc.query(delData,[params.id],function(err,result){
            if(result){
                result={
                    code:200,
                    msg:'删除数据成功'
                }
            }
            res.json(result);
            suc.release()
        })
    })
})
//插入数据
router.post('/add', function(req, res, next) {
    var params=req.body;
    console.log(params)
    pool.getConnection(function(err,suc){
        suc.query(addData,[params.name,params.phone],function(err,result){
            if(result){ //数据库有返回数据
                result={    //返回数据与格式
                    code:200,
                    msg:'新增数据成功'
                }
            }
            res.json(result);   //响应返回json数据
            suc.release();  //关闭数据库连接
        })
    })
});

router.get('/list', function(req, res, next) {
    pool.getConnection(function(err,suc){
        suc.query(selAll,[],function(err,result){
            if(result){ //数据库有返回数据
                result={    //返回数据与格式
                    code:200,
                    msg:'获取测试列表成功',
                    data:result
                }
            }
            res.json(result);   //响应返回json数据
            suc.release();  //关闭数据库连接
        })
    })
});

//响应post
router.post('/list', function(req, res, next) {
    var id=req.body.id; //通过req的body拿到post的id
    pool.getConnection(function(err,suc){
        suc.query(selId,[id],function(err,result){
            if(result){ //数据库有返回数据
                result={    //返回数据与格式
                    code:200,
                    msg:'获取单个测试列表成功',
                    data:result
                }
            }
            res.json(result);   //响应返回json数据
            suc.release();  //关闭数据库连接
        })
    })
});
Vue代码更新

html

<template>
  <div class="hello">
    <ul>
      <li>
        <el-input placeholder="请输入姓名" v-model="name">
          <template slot="prepend">姓名:</template>
        </el-input>
      </li>
      <li>
        <el-input placeholder="请输入电话" v-model="phone">
          <template slot="prepend">电话:</template>
        </el-input>
      </li>
      <li>
        <el-button type="primary" plain @click="add">添加</el-button>
      </li>
    </ul>
    <el-table class="user_table"
      :data="userList"
      border>
      <el-table-column
        fixed
        prop="Id"
        label="用户ID">
      </el-table-column>
      <el-table-column
        prop="u_name"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="u_phone"
        label="电话">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
          <el-button @click="edit(scope)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisible">
      <div class="update">
        <el-input placeholder="请输入姓名" v-model="edit_name">
          <template slot="prepend">姓名:</template>
        </el-input>
        <el-input placeholder="请输入电话" v-model="edit_phone">
          <template slot="prepend">电话:</template>
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editY()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

script

import axios from 'axios';
export default {
  name: 'HelloWorld',
  data () {
    return {
      // msg: 'Welcome to Your Vue.js App'
      name:'',
      phone:'',
      userList:[],
      dialogVisible:false,
      edit_name:'',
      edit_phone:'',
      edit_id:''
    }
  },
  mounted(){
    this.get()
  },
  methods:{
    editY(){
      var this_=this;
      axios.post('/users/edit',{
        id:this.edit_id,
        name:this.edit_name,
        phone:this.edit_phone
      }).then(function(res){
        console.log(res)
        this_.dialogVisible=false;
        this_.get()
      }).catch(function(err){
        console.log(err)
      })
    },
    edit(row){
      var this_=this,index=row.$index;
      this_.dialogVisible=true;
      var p_id=this_.userList[index].Id,
          p_name=this_.userList[index].u_name,
          p_phone=this_.userList[index].u_phone;
      this_.edit_id=p_id;
      this_.edit_name=p_name;
      this_.edit_phone=p_phone;
    },
    get(){
      var this_=this;
      axios.get('/users/list').then(function(res){
        this_.userList=res.data.data

      }).catch(function(err){
        console.log(err)
      })
    },
    add(){
      var this_=this;
      // var name=,phone=;
      axios.post('/users/add',{
        name:this.name,
        phone:this.phone
      }).then(function(res){
        console.log(res)
        this_.get()
      }).catch(function(err){
        console.log(err)
      })
    },
    del(row){
      var this_=this;
      axios.post('/users/del',{
        id:row.Id
      }).then(function(res){
        this_.get()
      }).catch(function(err){
        console.log(err)
      })
    }
  }
}

完整代码 ↓ ↓ ↓ ↓
github地址:https://github.com/jgsrty/jianshu_node
码云地址:https://gitee.com/RtyXmd/jianshu_node
测试代码写完了,接下来会重新部署一个,对node和vue代码进行优化与整理,下一步增加聊天室、完善登陆与注册(session,加密等)、评论、上传文件图片、爬虫爬取资源。。。
规划整理前后端代码(五)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 8,119评论 1 4
  • 简说Vue (组件库) https://github.com/ElemeFE/element" 饿了么出品的VUE...
    Estrus丶阅读 5,732评论 0 1
  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 4,866评论 0 1
  • 分享歌词: 一场雨 把我困在这里,你冷漠的表情 会让我伤心,六月的雨 就是无情的你,伴随着点点滴滴 痛击我心里,H...
    AA黎夏夏的美好时光阅读 2,994评论 0 1
  • 生活中,人们对于奇葩的人或事,总是会用一句“你以为是拍戏吗”来表达那种夸张和不可思议。然而,事实就正应了那...
    ZS佐神阅读 1,267评论 0 2