javascript - 如何解决[Vue warn] : Avoid mutating a prop directly since the value will be overwritten on vue. js 2?

标签 javascript vue.js vuejs2 vue-component

我的观点是这样的:

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

我的明星组件是这样的:

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

当我点击星标时,控制台出现这样的错误:

[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:\xampp\htdocs\myshop\resources\assets\js\components\Star.vue)

我该如何解决?

最佳答案

您需要使用 getter 和 setter 进行计算,然后使用 $emit 更新属性,例如:

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

关于javascript - 如何解决[Vue warn] : Avoid mutating a prop directly since the value will be overwritten on vue. js 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42522266/

相关文章:

javascript - 如何在android上获取javascript生成的html

javascript - vue warn - 避免直接修改 prop,因为只要父组件重新渲染,该值就会被覆盖

javascript - 检查 Vue 中是否为 null 或空格

javascript - 控制台警告 : component lists rendered with v-for should have explicit keys

javascript - 组件是否应该将其父组件的文件名作为其自身文件名的一部分?

javascript - 从 Vue.Js 中的下拉列表中获取项目属性

javascript - Blur.js 中的缓存前缀是什么?

javascript - wkhtmltopdf 第二页的页边距

javascript - 折叠嵌套的 ng-repeat 创建的 div

vue.js - 使用 VueJS 在图表上绘制异步信息