最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Vue結合后臺的列表增刪改案例

 更新時間:2018年08月21日 10:00:35   作者:鎮(zhèn)屌要逆襲  
這篇文章主要介紹了詳解Vue結合后臺的增刪改案例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

首先列表內容還是與之前的列表內容類似,不過此處我們會采用Vue中數(shù)據(jù)請求的方式來實現(xiàn)數(shù)據(jù)的增刪。那么我們使用的Vue第三方組件就是vue-resource,vue發(fā)起請求的方式與jQuery的ajax相似,組要是請求地址與參數(shù)。和方法

首先我們先看到的是列表請求

獲取列表

        <table class=" table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th>Id</th>
              <th>Name</th>
              <th>CTime</th>
              <th>Operation</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in list" :key="item.id">
              <td>{{ item.id }}</td>
              <td>{{item.title}}</td>
              <td>{{item.description}}</td>
              <td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click.prevent="del(item.id)">刪除</a></td>
    
            </tr>
          </tbody>
          
        </table>

在methods中獲取到的加入獲取數(shù)據(jù)的list方法,使用get請求

        getList(){
          this.$http.get('list').then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.list = result.data
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        },

需要注意的是,使用vue-resource的請求獲取的數(shù)據(jù),都封裝在回調數(shù)據(jù)的body域中,同時我們需要在Vue組件的created生命周期函數(shù)中加入使用該方法來渲染頁面

      created(){
      //在其他方法中調用定義的方法使用this關鍵字
          this.getList();
      },

增加和刪除元素的方法與此類似,這里給出詳細代碼,不做講解

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="lib/vue-2.4.0.js" ></script>
    <script type="text/javascript" src="lib/vue-resource-1.3.4.js"></script>
    <link rel="stylesheet" href="lib/bootstrap-3.3.7.css" rel="external nofollow" rel="external nofollow" />
  </head>
  <body>
    <div id="app">
      
      <div class="panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title">添加品牌</h3>
        </div>
      <div class="panel-body form-inline">
        <label>
          Id:<input type="text" v-model="id" class="form-control" />
        </label>
        <label>
          Name:
          <input type="text" v-model="title" class="form-control" />
        </label>
        <label>
          關鍵字
        </label>
        <input type="text" v-model="description" class="form-control"/>
        <input type="button" value="添加" class="btn btn-primary" @click="add()"/>

      </div>       
      </div>
        <table class=" table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th>Id</th>
              <th>Name</th>
              <th>CTime</th>
              <th>Operation</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in list" :key="item.id">
              <td>{{ item.id }}</td>
              <td>{{item.title}}</td>
              <td>{{item.description}}</td>
              <td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click.prevent="del(item.id)">刪除</a></td>
    
            </tr>
          </tbody>
          
        </table>
    </div>

  <script>
    var vm = new Vue({
      el:'#app',
      data:{
        id:"",
        title:"",
        description:"",
        list:[],

      },
      created(){
          this.getList();
      },
      methods:{

        getList(){
          this.$http.get('http://localhost:8080/list').then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.list = result.data
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        },
        add(){
          this.$http.post('http://localhost:8080/submit',{id:this.id,title:this.title,description:this.description},{emulateJSON:true}).then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.getList();
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        },
        del(id){
            this.$http.get('http://localhost:8080/del/'+id,{emulateJSON:true}).then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.getList();
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        }
      }
      
    })
  </script>
  </body>
</html>

上述代碼中有兩個地方略顯啰嗦,第一個是url,第二個是傳遞Json數(shù)據(jù)之后對json的處理,vue-resource 提供了兩個簡化的操作,

url簡化

我們可以在定義Vue對象之前使用

​ Vue.http.options.root=http://localhost:8080/;

來定義請求的基礎url,然后直接使用請求本身的url就可以了,但是需要注意的是兩段url連接起來之后,不允許出現(xiàn)‘//',否則會出問題,一般我會采用基礎url最后多‘/',而自定義的url則無

還有一個是對傳遞數(shù)據(jù)的參數(shù)的簡化,

我們可以在定義Vue對象之前使用

Vue.http.options.emulateJSON = true;

這樣我們在請求時即可默認有此參數(shù),請求的時候就不用加上這個參數(shù)了。

經過簡化,上述代碼被簡化為

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="lib/vue-2.4.0.js" ></script>
    <script type="text/javascript" src="lib/vue-resource-1.3.4.js"></script>
    <link rel="stylesheet" href="lib/bootstrap-3.3.7.css" rel="external nofollow" rel="external nofollow" />
  </head>
  <body>
    <div id="app">
      
      <div class="panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title">添加品牌</h3>
        </div>
      <div class="panel-body form-inline">
        <label>
          Id:<input type="text" v-model="id" class="form-control" />
        </label>
        <label>
          Name:
          <input type="text" v-model="title" class="form-control" />
        </label>
        <label>
          關鍵字
        </label>
        <input type="text" v-model="description" class="form-control"/>
        <input type="button" value="添加" class="btn btn-primary" @click="add()"/>

      </div>       
      </div>
        <table class=" table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th>Id</th>
              <th>Name</th>
              <th>CTime</th>
              <th>Operation</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in list" :key="item.id">
              <td>{{ item.id }}</td>
              <td>{{item.title}}</td>
              <td>{{item.description}}</td>
              <td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click.prevent="del(item.id)">刪除</a></td>
    
            </tr>
          </tbody>
          
        </table>
    </div>

  <script>
    Vue.http.options.root="http://localhost:8080/";
     Vue.http.options.emulateJSON = true;
    var vm = new Vue({
      el:'#app',
      data:{
        id:"",
        title:"",
        description:"",
        list:[],

      },
      created(){
          this.getList();
      },
      methods:{

        getList(){
          this.$http.get('list').then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.list = result.data
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        },
        add(){
          console.log("1");
          this.$http.post('submit',{id:this.id,title:this.title,description:this.description}).then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.getList();
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        },
        del(id){
          console.log(2);
            this.$http.get('del/'+id).then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.getList();
              
            }else{
              alert("獲取數(shù)據(jù)失敗");
            }
          })
        }
      }
      
    })
  </script>
  </body>
</html>

此案例后臺為我使用mybatis和springboot所做的簡單后臺,大家也可以自行操作。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue實現(xiàn)頁面添加水印效果

    vue實現(xiàn)頁面添加水印效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)頁面添加水印效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue使用canvas實現(xiàn)移動端手寫簽名

    vue使用canvas實現(xiàn)移動端手寫簽名

    這篇文章主要為大家詳細介紹了基于vue使用canvas實現(xiàn)移動端手寫簽名,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Vue中使用Canvas實現(xiàn)繪制二維碼

    Vue中使用Canvas實現(xiàn)繪制二維碼

    這篇文章主要為大家詳細介紹了如何在Vue中使用Canvas實現(xiàn)繪制二維碼,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2007-02-02
  • vuejs簡單驗證碼功能完整示例

    vuejs簡單驗證碼功能完整示例

    這篇文章主要介紹了vuejs簡單驗證碼功能,結合完整實例形式分析了vue.js驗證碼的生成、顯示、校驗等相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • Vue express鑒權零基礎入門

    Vue express鑒權零基礎入門

    這篇文章主要介紹了詳解express鑒權,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Vue實現(xiàn)拖拽改變列表順序詳解

    Vue實現(xiàn)拖拽改變列表順序詳解

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)拖拽改變列表順序的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-04-04
  • vue實現(xiàn)設置載入動畫和初始化頁面動畫效果

    vue實現(xiàn)設置載入動畫和初始化頁面動畫效果

    今天小編就為大家分享一篇vue實現(xiàn)設置載入動畫和初始化頁面動畫效果,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue 內聯(lián)樣式style中的background用法說明

    vue 內聯(lián)樣式style中的background用法說明

    這篇文章主要介紹了vue 內聯(lián)樣式style中的background用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue自定義loader將行內樣式px轉rem適配

    vue自定義loader將行內樣式px轉rem適配

    這篇文章主要為大家介紹了vue自定義loader將行內樣式px轉rem適配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • vue3 axios安裝及使用示例詳解

    vue3 axios安裝及使用示例詳解

    這篇文章主要介紹了vue3 axios安裝及使用示例代碼,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-04-04

最新評論

城市| 疏勒县| 桓仁| 安福县| 汉阴县| 饶阳县| 黎平县| 扎兰屯市| 化德县| 崇礼县| 松江区| 长海县| 延津县| 黄浦区| 宜州市| 芜湖县| 商河县| 漳州市| 霍山县| 巴林左旗| 聂拉木县| 壤塘县| 铜梁县| 嘉禾县| 嘉峪关市| 甘谷县| 当阳市| 莱芜市| 罗江县| 大田县| 建昌县| 大竹县| 北安市| 丹巴县| 靖宇县| 白水县| 大竹县| 汉阴县| 榆社县| 光泽县| 京山县|