There is a variable that is associated with the field (using vue). How to call a function every time a variable changes?

    3 answers 3

    try this

    methods: { changeInput: function () { console.log(this.model); } } 
     <input @change="changeInput()" v-model="model"> 

      For observation, Vue has excellent methods; watch , for example:

       new Vue({ el: '#root', data: { input: '', watchInput: '', }, watch: { input: function () { this.watchInput = `New watchInput value: ${Math.random()}!`; } } }); 
       <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script> <div id='root'> <input type='text' v-model='input' /> <div>{{ watchInput }}</div> </div> 

        You can try to wrap in computed, the method will be called when the varName variable changes

         data() { return { dataName: null } }, computed: { varName() { this.yourMethod(); return this.dataName; } }, methods: { yourMethod() { ... } }