使用带有选择标记的@click方法来更改计算属性的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用带有选择标记的@click方法来更改计算属性的值相关的知识,希望对你有一定的参考价值。
我想做的是:
- 在文本框中输入情感,并在选择框中输入情感的强度。蓝色按钮增添了情感。
- 在重估部分,我想重估那种情绪的强度。
在重新计算中,我正在遍历计算的属性rerateEmotions
,该属性具有包含emotion
和intensity
属性的对象数组。我想做的是使用select标签更改计算属性上每种情感的强度:
编辑
[好,我发现我需要在重新定价@click
上将@change
更改为<select>
现在,我只想弄清楚如何从“评价情感”和“评价情感”区域中获得单独的评分。
const app = new Vue({
el: "#app",
data: function() {
return {
emotion: "",
intensity: null,
newIntensity: null,
emotionIntensity: []
};
},
computed: {
rerateEmotions() {
return this.emotionIntensity;
}
},
methods: {
rerateIntensity(id) {
return (this.rerateEmotions[id].intensity = this.newIntensity);
},
openEmotion(id) {
this.emotionId = id;
},
addEmotionIntensity() {
var emotionIntensity = {
emotion: this.emotion,
intensity: this.intensity
};
this.emotionIntensity.push(emotionIntensity);
this.emotion = "";
this.intensity = null;
},
removeEmotion(id) {
if (id > -1) {
this.emotionIntensity.splice(id, 1);
}
},
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="row mb-3 pr-4">
<div class="col-6">
<h1>Rate</h1>
<input v-model="emotion" type="text" placeholder="Enter emotion here." class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>
<div class="col-5 select-outline">
<select class="mdb-select md-form md-outline colorful-select dropdown-primary" v-model="intensity">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<button @click="addEmotionIntensity()" type="button" class="col-1 btn btn-primary">+</button>
</div>
<ul>
<li v-for="(emotion, index) in emotionIntensity">
<b>{{emotion.emotion}} : {{emotion.intensity}}</b>
<button type="button" @click="removeEmotion(index)">
X
</button>
</li>
</ul>
<hr>
<div class="form-group">
<label for="exampleFormControlTextarea1"><h5>Rerate</h5></label>
<li v-for="(emotion, index) in rerateEmotions">
<b>{{emotion.emotion}} : {{emotion.intensity}}</b>
<select class="mdb-select md-form md-outline colorful-select dropdown-primary" @change="rerateIntensity(index)">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</li>
<hr>
</div>
</div>
答案
如果您需要两个强度等级,则每个对象可能只有两个属性。例如intensity
和newIntensity
。在下面的示例中,当第一次创建条目时,我已经将两者初始化为相同的值,但是如果愿意的话,第二个属性可以保留为空。
[计算属性rerateEmotions
似乎没有做任何事情,因此我删除了它,转而直接使用emotionIntensity
。也许目的是创建一个副本,但是使用计算属性不会产生这种效果,它只会是相同的数组。
对于重速率下拉列表,我使用了v-model
,尽管没有理由不愿意使用事件监听器。关键是我正在使用newIntensity
属性作为第二个值。
const app = new Vue({
el: "#app",
data: function() {
return {
emotion: "",
intensity: null,
newIntensity: null,
emotionIntensity: []
};
},
methods: {
openEmotion(id) {
this.emotionId = id;
},
addEmotionIntensity() {
var emotionIntensity = {
emotion: this.emotion,
intensity: this.intensity,
newIntensity: this.intensity
};
this.emotionIntensity.push(emotionIntensity);
this.emotion = "";
this.intensity = null;
},
removeEmotion(id) {
if (id > -1) {
this.emotionIntensity.splice(id, 1);
}
},
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="row mb-3 pr-4">
<div class="col-6">
<h1>Rate</h1>
<input v-model="emotion" type="text" placeholder="Enter emotion here." class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>
<div class="col-5 select-outline">
<select class="mdb-select md-form md-outline colorful-select dropdown-primary" v-model="intensity">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<button @click="addEmotionIntensity()" type="button" class="col-1 btn btn-primary">+</button>
</div>
<ul>
<li v-for="(emotion, index) in emotionIntensity">
<b>{{emotion.emotion}} : {{emotion.intensity}}</b>
<button type="button" @click="removeEmotion(index)">
X
</button>
</li>
</ul>
<hr>
<div class="form-group">
<label for="exampleFormControlTextarea1"><h5>Rerate</h5></label>
<li v-for="(emotion, index) in emotionIntensity">
<b>{{emotion.emotion}} : {{emotion.newIntensity}}</b>
<select v-model="emotion.newIntensity" class="mdb-select md-form md-outline colorful-select dropdown-primary">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</li>
<hr>
</div>
</div>
以上是关于使用带有选择标记的@click方法来更改计算属性的值的主要内容,如果未能解决你的问题,请参考以下文章