watch:{ obj:{ deep: true, handler(){ this.praiseLen = this.obj["praise_len"] } } }
时间: 2024-06-05 11:12:33 浏览: 13
这段代码是 Vue.js 中的一个监听器(watcher),它会监听一个对象(obj)中的 deep 属性的变化,一旦 deep 属性的值发生改变,它就会执行 handler 函数。在 handler 函数中,它将 obj 对象中的 praise_len 属性的值赋值给 Vue 实例中的 praiseLen 属性。这样,当 obj 中的 deep 属性或 praise_len 属性的值发生改变时,praiseLen 属性也会相应地更新。
相关问题
解析一下const obj = { 2: 5, 3: 6, 4: 7 } obj[Symbol.iterator] = function () { return { next: function () { if (this._countDown === 3) { return { value: this._countDown, done: true } } this._countDown = this._countDown + 1 return { value: obj[this._countDown], done: false } }, _countDown: 0, } } for (const i of obj) { console.log(i) }
这段代码中定义了一个名为 `obj` 的对象,它有三个属性 `2`、`3` 和 `4`,分别对应着值 `5`、`6` 和 `7`。然后通过 `obj[Symbol.iterator]` 方法给 `obj` 对象添加了一个迭代器,这个迭代器包含一个 `_countDown` 属性和一个 `next` 方法。迭代器的 `next` 方法会根据 `_countDown` 属性的值返回下一个迭代的对象,其中 `value` 属性为 `obj` 对象中下一个键对应的值,`done` 属性表示是否已经迭代完成。当 `_countDown` 属性的值为 `3` 时,迭代器返回 `{ value: this._countDown, done: true }`,表示迭代已经完成。否则,迭代器会将 `_countDown` 属性的值加 1,然后返回 `{ value: obj[this._countDown], done: false }`,表示迭代还未完成。最后,使用 `for...of` 循环遍历 `obj` 对象,循环内部通过迭代器的 `next` 方法获取下一个值并输出。输出的结果为 `5`、`6` 和 `7`。
change_praise(obj) { var user_id = this.user.user_id; var query = { source_table: "dish_information", source_field: "dish_information_id", source_id: this.obj["dish_information_id"], user_id }; var _this = this; _this.obj.praise_len = parseInt(_this.obj.praise_len) // 点赞状态 if (this.state_praise) { this.state_praise = false; this.$get('~/api/praise/del?', query, (res) => { if(res.result){ var praise_len = _this.obj.praise_len-1 this.$post('~/api/dish_information/set?dish_information_id=' + _this.obj["dish_information_id"], { praise_len }, (res) => { if(res.result){ console.log("添加点赞数状态:" ,res.result); } else if(res.error){ console.error(res.error); } }); this.$toast("取消点赞"); } else if (res.error){ this.$toast(res.error.message); console.error(res.error); } }); } else { this.state_praise = true; this.$post('~/api/praise/add?', query, (res) => { if (res.result) { var praise_len = _this.obj.praise_len+1 this.$post('~/api/dish_information/set?dish_information_id=' + _this.obj["dish_information_id"], { praise_len }, (res) => { if(res.result){ console.log("添加点赞数状态:" ,res.result); } else if(res.error){ console.error(res.error); } }); this.$toast("点赞成功"); } else if (res.error) { this.$toast(res.error.message); console.error(res.error); } }); }; },
这是一个 Vue.js 中的方法,用于处理点赞操作。当用户点击点赞按钮时,会调用该方法。它会根据当前点赞状态来决定是添加点赞还是取消点赞,并相应地更新点赞数和状态。具体实现过程中,它会向服务器发送请求来添加或删除点赞,并通过回调函数来处理服务器返回的结果。其中,obj 是一个包含菜品信息的对象,state_praise 是一个布尔值,表示当前的点赞状态。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)