【发布时间】:2021-01-02 05:22:40
【问题描述】:
我收到警告“避免直接改变道具”,我知道这可以通过Vue Official Documentation 中提到的数据属性或计算使用来解决。但我不知道如何将我的代码更改为任何这些方法。请帮助我使用正确的代码来消除此警告。
我的代码如下所示:
<template>
<div class="track-rating">
<span :key="note" v-for="note in maxNotes" :class="{ 'active': note <= rating || note <= hoveredNote }" @mouseover="hoveredNote = note" @mouseleave="hoveredNote = false" @click="rate(note)" class="material-icons mr-1">
audiotrack
</span>
</div>
</template>
<script>
export default {
name: "Rating",
props: {
rating: {
type: Number,
required: true
},
maxNotes: {
type: Number,
default: 3
},
hasCounter: {
type: Boolean,
default: true
},
itemId: {
type: String
}
},
data() {
return {
hoveredNote: false
};
},
methods: {
rate(note) {
if (typeof note === 'number' && note <= this.maxNotes && note >= 0)
this.rating = this.rating === note ? note - 1 : note
this.$emit('onRate', this.itemId, this.rating);
}
}
};
【问题讨论】:
标签: vue.js vuejs2 vue-component