我收到警告:
app.js:87486 [Vue警告]:避免直接更改道具,因为每当父组件重新渲染时,该值就会被覆盖。而是使用基于属性值的数据或计算属性。道具被变异:“ likescount”
我的剑
<path-like-toggle likescount="{{$path->likeCount}}" isliked="{{$path->canBeunLiked()}}" path="{{$path->slug}}" type="{{$path->type}}" ></path-like-toggle>
Vue代码
props:['isliked','path','type','likescount']
,
methods:{
like () {
axios.post(`/${this.type}/${this.path}/like/`)
this.likingStatus = true;
this.likescount ++;
},
unlike () {
axios.post(`/${this.type}/${this.path}/unlike/`)
this.likingStatus = false;
this.likescount --;
},
}
最佳答案
从道具中初始化数据属性,然后对其进行操作。
export default {
data() {
return {
numberOfLikes: this.likescount
}
},
props: [
'isliked',
'path',
'type',
'likescount'
],
methods:{
like() {
axios.post(`/${this.type}/${this.path}/like/`);
// ...
this.numberOfLikes++;
},
unlike() {
axios.post(`/${this.type}/${this.path}/unlike/`);
// ...
this.numberOfLikes--;
},
}
}
关于javascript - 避免直接改变 Prop ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56752919/