【问题标题】:Accessing vue instance in controller's function在控制器的函数中访问 vue 实例
【发布时间】:2015-09-11 23:56:52
【问题描述】:

控制器:

Vue.directive('fetch',function(value,scope){
value["url"].isLiteral = true;
qwest.post(value["url"]).then(
    function(response){
        console.log(response);
        if (response["CODE"] == 1){
            console.log(response["RESULT"]);
            this.result = response["RESULT"];
            this.error = "";
        }else{
            this.result = "";
            this.error = response["ERROR"];
        }
    }
).catch(
        function(e,response){
            this.error = response["ERROR"];
        }
    );
})

型号:

var test = new Vue({
el : "#test",
data : {
    code : "",
    result : "",
    error : ""
    }
})

所以,如果我使用this.result,它超出了范围,但是如果我将它引用为test.result,那么它会被访问并更新值,那么访问 vue 实例的通用方法是什么?

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    我找到了解决方案, 可以使用指令对象中可用的属性,this.vm 实际上是指令的 vue 模型,因此可以使用 this.vm 并在调用函数之前将其分配给某个变量,从而使用名称this.vm 作为单例函数调用分配给的变量。

    Vue.directive('fetch',function(value){
    value["url"].isLiteral = true;
    var model = this.vm;
    qwest.post(value["url"]).then(
        function(response){
            console.log(response);
            if (response["CODE"] == 1){
                console.log(response["RESULT"]);
                model.result = response["RESULT"];
                model.error = "";
            }else{
                model.result = "";
                model.error = response["ERROR"];
            }
        }
    ).catch(
            function(e,response){
                model.error = response["ERROR"];
            }
        );
    })
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 2012-12-06
      • 2013-01-08
      • 2016-10-25
      • 2017-08-07
      • 2021-08-25
      • 1970-01-01
      相关资源
      最近更新 更多