当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 详解Vue结合后台的列表增删改案例

详解Vue结合后台的列表增删改案例

2018年09月23日  | 移动技术网IT编程  | 我要评论
首先列表内容还是与之前的列表内容类似,不过此处我们会采用vue中数据请求的方式来实现数据的增删。那么我们使用的vue第三方组件就是vue-resource,vue发起请求的

首先列表内容还是与之前的列表内容类似,不过此处我们会采用vue中数据请求的方式来实现数据的增删。那么我们使用的vue第三方组件就是vue-resource,vue发起请求的方式与jquery的ajax相似,组要是请求地址与参数。和方法

首先我们先看到的是列表请求

获取列表

        <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中获取到的加入获取数据的list方法,使用get请求

        getlist(){
          this.$http.get('list').then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.list = result.data
              
            }else{
              alert("获取数据失败");
            }
          })
        },

需要注意的是,使用vue-resource的请求获取的数据,都封装在回调数据的body域中,同时我们需要在vue组件的created生命周期函数中加入使用该方法来渲染页面

      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("获取数据失败");
            }
          })
        },
        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("获取数据失败");
            }
          })
        },
        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("获取数据失败");
            }
          })
        }
      }
      
    })
  </script>
  </body>
</html>

上述代码中有两个地方略显啰嗦,第一个是url,第二个是传递json数据之后对json的处理,vue-resource 提供了两个简化的操作,

url简化

我们可以在定义vue对象之前使用

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

来定义请求的基础url,然后直接使用请求本身的url就可以了,但是需要注意的是两段url连接起来之后,不允许出现‘//',否则会出问题,一般我会采用基础url最后多‘/',而自定义的url则无

还有一个是对传递数据的参数的简化,

我们可以在定义vue对象之前使用

vue.http.options.emulatejson = true;

这样我们在请求时即可默认有此参数,请求的时候就不用加上这个参数了。

经过简化,上述代码被简化为

<!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("获取数据失败");
            }
          })
        },
        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("获取数据失败");
            }
          })
        },
        del(id){
          console.log(2);
            this.$http.get('del/'+id).then(result=>{
            var result =result.body;
            if(result.code ===200){
              this.getlist();
              
            }else{
              alert("获取数据失败");
            }
          })
        }
      }
      
    })
  </script>
  </body>
</html>

此案例后台为我使用mybatis和springboot所做的简单后台,大家也可以自行操作。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网