如何解决 [Vue warn]:避免直接修改 prop,因为值将在 vue.js 2 上被覆盖?

How to solve [Vue warn]: Avoid mutating a prop directly since the value will be overwritten on vue.js 2?(如何解决 [Vue warn]:避免直接修改 prop,因为值将在 vue.js 2 上被覆盖?)

本文介绍了如何解决 [Vue warn]:避免直接修改 prop,因为值将在 vue.js 2 上被覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的看法是这样的:

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

我的明星组件是这样的:

My star component is like this :

<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>

我的css是这样的:

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 exist error on the console like this :

[Vue 警告]:避免直接改变一个 prop,因为它的值是每当父组件重新渲染时被覆盖.相反,使用基于道具值的数据或计算属性.道具存在突变:价值"(在C:xampphtdocsmyshop esourcesassetsjscomponentsStar.vue)

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value" (found in at C:xampphtdocsmyshop esourcesassetsjscomponentsStar.vue)

我该如何解决?

推荐答案

你需要使用 getter 和 setter 进行计算,然后使用 $emit 来更新 prop eg:

You need to make the computed with a getter and a setter and then use $emit to update the prop e.g:

    computed: {
        starValue:{ 
            get:function () {
                return this.temp_value
            },
            set: function(newValue){
                // $emit is the correct way to update props:
                this.$emit('update:value', newValue);
            }
        }
    }

这篇关于如何解决 [Vue warn]:避免直接修改 prop,因为值将在 vue.js 2 上被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何解决 [Vue warn]:避免直接修改 prop,因为值将在 vue.js 2 上被覆盖?

基础教程推荐