使用vue资源将vue数据作为主体发布到post请求
<script>
//var VueValidator = require ('vue-validator');
new Vue({
el: '#app',
data: {
email: '',
password: '',
_token: '{{csrf_token()}}',
uid: '{{$row->id}}'
},
methods: {
changeCredentials: function (e) {
console.log(this.email); --> **RETURNS EMPTY**
var resource = this.$resource('myurl/{id}');
resource.save({id: this.uid}, {body: function () {
console.log(this.data);
return this.data;
}}).then((response) => {
// success callback
console.log(response);
}, (response) => {
// error callback
});
//alert('here');
}
}
});
</script>你能告诉我为什么this.email不能工作而this.uid能工作吗?谢谢
发布于 2016-09-01 16:02:21
这是因为当您访问this.data时,您处于一个回调中,它会更改您当前所在的作用域,而this实际上并不是您的Vue组件。
通过设置_this,一旦确定了更改的范围并进入回调,就可以访问该变量。
changeCredentials: function () {
var resource = this.$resource('myurl/{id}');
var _this = this;
resource.save({id: _this.uid}, {body: function () {
console.log(_this.data);
return _this.data;
}});
}https://stackoverflow.com/questions/39263381
复制相似问题