在Ajax函数中访问Vue.js组件属性

viv*_*ino 5 ajax jquery vue.js

以下代码引用一个组件函数,该函数从URL获取数据并尝试将该数据设置为属性.它不起作用,似乎this无法从ajax clousure范围访问.

var MyComp = Vue.extend({
props: {
        html_prop: {}
        ....
},
methods: {
        fetchCondiciones: function(url){

            $.ajax({
                    type: "GET",
                    url: url,
                    cache: false,
                    data: vardata ,
                    success: function(data,status,error) {
                        if( data!=''){
                            this.html_prop = data;
                        }
                    },
                    error: function(data,status,error){
                        alert(error);
                    }
            });

        }

        ...
    }
})
Run Code Online (Sandbox Code Playgroud)

我怎么才能this进入?

nil*_*ils 10

你需要.bindthis背景下,作为回调不叫自然在组件的情况下:

var MyComp = Vue.extend({
props: {
        html_prop: null,
},
        // ....
        fetchCondiciones: function(url){

            $.ajax({
                    type: "GET",
                    url: url,
                    cache: false,
                    data: vardata,
                    success: function(data,status,error) {
                        if(data != ''){
                            this.html_prop = data;
                        }
                    }.bind(this), // bind this here
                    error: function(data,status,error){
                        alert(error);
                    }.bind(this) // bind this here
            });

        }

        // ...
});
Run Code Online (Sandbox Code Playgroud)

您可以.bind在此处了解更多信息:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind


cra*_*bly 10

正如已经回答的那样,.bind将解决此问题,但是,我喜欢使用不同的方法,并this在任何Ajax调用或嵌套函数之前存储在变量中.代码在方法内部增长时非常有用.它也更容易阅读.

例如,您可以保存this为var self.然后self在该方法内的任何地方使用都没有问题.

var MyComp = Vue.extend({
props: {
    html_prop: null,
},
    // ....
    fetchCondiciones: function(url){

        var self = this;

        $.ajax({
                type: "GET",
                url: url,
                cache: false,
                data: vardata,
                success: function(data,status,error) {
                    if(data != ''){
                        self.html_prop = data;
                    }
                }
                error: function(data,status,error){
                    alert(error);
                }
        });

    }

    // ...
});
Run Code Online (Sandbox Code Playgroud)

更新:

今天我们可以使用ES6箭头函数语法.

this函数内部的值由周围的范围决定,不需要创建新变量,或者使用bind:

    // ....
    fetchCondiciones: url => {
        $.ajax({
                type: "GET",
                url: url,
                cache: false,
                data: vardata,
                success: (data,status,error) => {
                    if(data != ''){
                        this.html_prop = data;
                    }
                }
                error: (data,status,error) => {
                    alert(error);
                }
        });
    }
Run Code Online (Sandbox Code Playgroud)

要么:

    // ....
    fetchCondiciones(url) {
        $.ajax({
                type: "GET",
                url: url,
                cache: false,
                data: vardata,
                success(data,status,error) {
                    if(data != ''){
                        this.html_prop = data;
                    }
                }
                error(data,status,error) {
                    alert(error);
                }
        });
    }
Run Code Online (Sandbox Code Playgroud)