简体   繁体   中英

vue.js prevent one data element from trigger update

Hi is there a way to stop triggering updated method in vue instance only for one element?. Here is my code,

vue instance..

           var vm = new Vue({
                el: '#el',
                data: {
                    cap: "",
                    radius: "",
                    var1: "",
                    var2: "",
                    var3: "",
                    items: null,
                }
                updated: function () {
                    axios.post('{{ url('car_result') }}', {data: this.$data})
                           .then(response => {
                            this.items = response.data;
                        });
                }
            });

This is my dom.

              <div id="el">
                    <input v-model="cap" type="text">
                    <input v-model="radius" type="text">
                    <input v-model="var1" type="text">
                    <input v-model="var2" type="text">
                    <input v-model="vat3" type="text">

                    <div class="item" v-for="item in items">
                        {{ item.id }}
                    </div>
                </div> 

Here I need to stop triggering updated method when items changed

How about grouping your input variables into a single input and then set up a watcher and update items only when the input changes ?

var vm = new Vue({
  el: '#el',
  data: {
    input: {
      cap: "",
      radius: "",
      var1: "",
      var2: "",
      var3: ""
    },
    items: null,
  },
  watch: {
    input: {
      handler (newInput) {
        axios.post('{{ url('car_result') }}', { data: newInput })
          .then(response => {
            this.items = response.data;
          });
      },
      deep: true
    }
  }
});

In your template:

<div id="el">
  <input v-model="input.cap" type="text">
  <input v-model="input.radius" type="text">
  <input v-model="input.var1" type="text">
  <input v-model="input.var2" type="text">
  <input v-model="input.vat3" type="text">

  <div class="item" v-for="item in items">
    {{ item.id }}
  </div>
</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM