How to solve [Vue warn]: Avoid muting prop directly, since the value will be overwritten in vue.js 2?

My opinion is this:

<div class="col-md-8">
    ...
        <star :value="{{ $data['rating'] }}"></star>
    ...
</div>

My star component is as follows:

<template>
    <span class="rating" :class='{"disable-all-rating": !!value}'>
        <template v-for="item in items">
            <label class="radio-inline input-star" :class="{'is-selected': ((starValue>= item.value) && starValue!= null)}">
                <input type="radio" class="input-rating" v-model="starValue" @click="rate(item.value)">
            </label>
        </template>
    </span>
</template>
<script>
    export default{
        props: {
            'value': null
        },
        computed: {
            starValue () {
                return this.temp_value
            }
        },
        data(){
            return{
                items: [
                    {value: 5},
                    {value: 4},
                    {value: 3},
                    {value: 2},
                    {value: 1}
                ],
                temp_value: null,
            }
        },
        methods:{
            rate: function (star) {           
               this.$http.post(window.BaseUrl + '/star', {star: star});                         
               this.temp_value = star;                         
            },
        }
    }
</script>

My css is as follows:

span.rating {
  direction: rtl;
  display: inline-block;
}

span.rating .input-star {
  background: url("../img/star.png") 0 -16px;
  padding-left: 0;
  margin-left: 0;
  width: 16px;
  height: 16px;
}

span.rating .input-star:hover, span.rating .input-star:hover ~ .input-star {
  background-position: 0 0;
}

span.rating .is-selected{
   background-position: 0 0;
}

span.rating .is-disabled{
   cursor: default;
}

span.rating .input-star .input-rating {
  display: none;
}

When I click the star, there is an error on the console:

[Vue warn]: Avoid mutating the propeller directly, as the value will be overwritten whenever the parent component re-displays. Instead, use data or a computed value based on the value of prop. Promotion mutated: "value" (found in C: \ XAMPP \ HTDOCS \ MyShop \ Resources \ assets \ JS \ Components \ Star.vue)

How can I solve it?

+4
source share
2

value.

return this.value = star;

, , .

v-model="value"

, , , value $data['rating'], , .

, , - , , , $emit, $data['rating'], .

. Vue .

+3

prop , , .

, , :

methods: {
  rate: function (star) {
    var self = this;
    if (!this.disabled) {
        this.$http.post(window.BaseUrl + '/star', {star: star}).then(function (response) {
            console.log('submitted');
        });
        this.temp_value = star;
        // return this.value = star; - remove this line
    }
  }
}

computed:

computed: {
  starValue () {
    return this.temp_value
  }
}
0

Source: https://habr.com/ru/post/1671092/


All Articles