對(duì)數(shù)據(jù)進(jìn)行一些基本操作(四)

接收post請(qǐng)求(vue+axios)解決跨域問(wèn)題(三)

效果預(yù)覽
最終效果預(yù)覽
node路由配置增刪改查
//查詢語(yǔ)句
var selAll='select * from list';
//根據(jù)post的id查詢
var selId='select * from list where id=?';
//插入數(shù)據(jù)
var addData='insert into list (u_name,u_phone) values (?,?)';
//刪除數(shù)據(jù)
var delData='delete from list where id=?';
//更新數(shù)據(jù)
var upData='update list set u_name=?,u_phone=? where id=?'
//更新數(shù)據(jù)
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:'更新數(shù)據(jù)成功'
                }
            }
            res.json(result);
            suc.release()
        })
    })
})
//刪除數(shù)據(jù)
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:'刪除數(shù)據(jù)成功'
                }
            }
            res.json(result);
            suc.release()
        })
    })
})
//插入數(shù)據(jù)
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){ //數(shù)據(jù)庫(kù)有返回?cái)?shù)據(jù)
                result={    //返回?cái)?shù)據(jù)與格式
                    code:200,
                    msg:'新增數(shù)據(jù)成功'
                }
            }
            res.json(result);   //響應(yīng)返回json數(shù)據(jù)
            suc.release();  //關(guān)閉數(shù)據(jù)庫(kù)連接
        })
    })
});

router.get('/list', function(req, res, next) {
    pool.getConnection(function(err,suc){
        suc.query(selAll,[],function(err,result){
            if(result){ //數(shù)據(jù)庫(kù)有返回?cái)?shù)據(jù)
                result={    //返回?cái)?shù)據(jù)與格式
                    code:200,
                    msg:'獲取測(cè)試列表成功',
                    data:result
                }
            }
            res.json(result);   //響應(yīng)返回json數(shù)據(jù)
            suc.release();  //關(guān)閉數(shù)據(jù)庫(kù)連接
        })
    })
});

//響應(yīng)post
router.post('/list', function(req, res, next) {
    var id=req.body.id; //通過(guò)req的body拿到post的id
    pool.getConnection(function(err,suc){
        suc.query(selId,[id],function(err,result){
            if(result){ //數(shù)據(jù)庫(kù)有返回?cái)?shù)據(jù)
                result={    //返回?cái)?shù)據(jù)與格式
                    code:200,
                    msg:'獲取單個(gè)測(cè)試列表成功',
                    data:result
                }
            }
            res.json(result);   //響應(yīng)返回json數(shù)據(jù)
            suc.release();  //關(guān)閉數(shù)據(jù)庫(kù)連接
        })
    })
});
Vue代碼更新

html

<template>
  <div class="hello">
    <ul>
      <li>
        <el-input placeholder="請(qǐng)輸入姓名" v-model="name">
          <template slot="prepend">姓名:</template>
        </el-input>
      </li>
      <li>
        <el-input placeholder="請(qǐng)輸入電話" 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="請(qǐng)輸入姓名" v-model="edit_name">
          <template slot="prepend">姓名:</template>
        </el-input>
        <el-input placeholder="請(qǐng)輸入電話" 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
測(cè)試代碼寫(xiě)完了,接下來(lái)會(huì)重新部署一個(gè)先较,對(duì)node和vue代碼進(jìn)行優(yōu)化與整理,下一步增加聊天室、完善登陸與注冊(cè)(session砾嫉,加密等)杉编、評(píng)論贞奋、上傳文件圖片赌厅、爬蟲(chóng)爬取資源。轿塔。特愿。
規(guī)劃整理前后端代碼(五)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市勾缭,隨后出現(xiàn)的幾起案子揍障,更是在濱河造成了極大的恐慌,老刑警劉巖俩由,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毒嫡,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡幻梯,警方通過(guò)查閱死者的電腦和手機(jī)兜畸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)礼旅,“玉大人膳叨,你說(shuō)我怎么就攤上這事洽洁《幌担” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵饿自,是天一觀的道長(zhǎng)汰翠。 經(jīng)常有香客問(wèn)我龄坪,道長(zhǎng),這世上最難降的妖魔是什么复唤? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任健田,我火速辦了婚禮,結(jié)果婚禮上佛纫,老公的妹妹穿的比我還像新娘妓局。我一直安慰自己,他們只是感情好呈宇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布好爬。 她就那樣靜靜地躺著,像睡著了一般甥啄。 火紅的嫁衣襯著肌膚如雪存炮。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,578評(píng)論 1 305
  • 那天蜈漓,我揣著相機(jī)與錄音穆桂,去河邊找鬼。 笑死融虽,一個(gè)胖子當(dāng)著我的面吹牛享完,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播有额,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼驼侠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了谆吴?” 一聲冷哼從身側(cè)響起倒源,我...
    開(kāi)封第一講書(shū)人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎句狼,沒(méi)想到半個(gè)月后笋熬,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡腻菇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年胳螟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片筹吐。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡糖耸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出丘薛,到底是詐尸還是另有隱情嘉竟,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站舍扰,受9級(jí)特大地震影響倦蚪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜边苹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一陵且、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧个束,春花似錦慕购、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至桩警,卻和暖如春可训,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捶枢。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工握截, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人烂叔。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓谨胞,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蒜鸡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胯努,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • ## 框架和庫(kù)的區(qū)別?> 框架(framework):一套完整的軟件設(shè)計(jì)架構(gòu)和**解決方案**。> > 庫(kù)(lib...
    Rui_bdad閱讀 2,910評(píng)論 1 4
  • 簡(jiǎn)說(shuō)Vue (組件庫(kù)) https://github.com/ElemeFE/element" 餓了么出品的VUE...
    Estrus丶閱讀 1,653評(píng)論 0 1
  • 基于Vue的一些資料 內(nèi)容 UI組件 開(kāi)發(fā)框架 實(shí)用庫(kù) 服務(wù)端 輔助工具 應(yīng)用實(shí)例 Demo示例 element★...
    嘗了又嘗閱讀 1,151評(píng)論 0 1
  • 分享歌詞: 一場(chǎng)雨 把我困在這里逢防,你冷漠的表情 會(huì)讓我傷心叶沛,六月的雨 就是無(wú)情的你,伴隨著點(diǎn)點(diǎn)滴滴 痛擊我心里忘朝,H...
    AA黎夏夏的美好時(shí)光閱讀 372評(píng)論 0 1
  • 生活中灰署,人們對(duì)于奇葩的人或事,總是會(huì)用一句“你以為是拍戲嗎”來(lái)表達(dá)那種夸張和不可思議局嘁。然而溉箕,事實(shí)就正應(yīng)了那...
    ZS佐神閱讀 160評(píng)論 0 2