[Vuejs]-Vue method not working from inside other method

2👍

try add var _this= this; use _this.loadContent();
or use app.loadContent();

getLocation(){
 var _this= this;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                this.position = position;
                _this.loadContent();
            }, function(error) {
                console.log(error);
            });
        }
    },

1👍

this refers to the object that called a function. In the present case, this is navigator.geolocation. You can override the calling object by calling bind on the function:

getLocation(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            this.position = position;
            this.loadContent();
        }.bind(this), function(error) {
            console.log(error);
        });
    }
}

Leave a comment