2👍
✅
On your method declaration, remove .bind(this)
. It will bind the method function to the window
object, and not the Vue instance.
In other words, when you use methods: { myMethod: function() { console.log(this); }.bind(this) }
, inside myMethod
the this
will be window
and not the Vue instance.
Below a quick demo of this:
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!'
},
methods: {
regularMethod: function () { console.log('regular method', this.message); },
bindThisMethod: function () { console.log('method with .bind(this)', this.message); }.bind(this)
},
created() {
this.regularMethod();
this.bindThisMethod();
}
})
<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<div id="app">
<p>{{ message }}</p>
</div>
Finally, here’s how you should modify your code:
methods: {
bbb: function() {
var self=this;
<!--var a={};-->
$.getJSON('http://127.0.0.1:8000/tasks/',function(task){
self.dbtask = task;
alert(self.dbtask[0].exercisepath);
});
// }.bind(this) // remove this
} // should be like this
}
Source:stackexchange.com