How to update data of a VueJS instance from a jQuery AJAX call?

后端 未结 3 1551
悲哀的现实
悲哀的现实 2021-01-01 21:48

I have a VueJS instance with some data :

var vm = new Vue({
    el: \'#root\',
    data: {
        id: \'\',
        name: {
            firstname: \"\", 
           


        
3条回答
  •  庸人自扰
    2021-01-01 22:08

    Another thing that plays a role here is how Vue.js updates the DOM. For details read this section in the documentation: Async Update Queue

    In short use "nexTick" callback to process your code after Vue.js is done updating the DOM.

    methods: {
       someMethod() {
          var self = this;
          $.ajax({
            url: 'http://...',
          }).done(function (data) {
            self.a = data.a;
            self.b = data.b;
            self.$nextTick(function () {
               // a and b are now updated
            }
          });
       }
    }
    

提交回复
热议问题