[英]console.log() does not show actual value
I am iterating over a list on which I used a @click function to update some other elements.我正在迭代一个列表,在该列表上我使用了 @click 函数来更新其他一些元素。 The elements indexes values are binded to selectedFixtures
.元素索引值绑定到selectedFixtures
。 I don't understand why, when I click on an element, it prints out []
and the second time I click on an element, it prints out the old value ex.: [1]
.我不明白为什么,当我点击一个元素时,它会打印出[]
而我第二次点击一个元素时,它会打印出旧值,例如: [1]
。 How can I work with the actual values in my function so it prints out [1]
on the first time?我如何处理函数中的实际值,以便它在第一次打印出[1]
?
<v-chip-group
v-model="selectedFixtures"
multiple
>
<v-chip v-for="(f, i) in filteredFixtures()" :key="i" @click="selectFixture(f, i)">
<div class="d-flex align-baseline">
<span class="font-weight-medium">{{ f.name }}</span>
</div>
</v-chip>
</v-chip-group>
methods: {
selectFixture(f, index) {
console.log(JSON.stringify(this.selectedFixtures));
if (this.selectedFixtures.length === 1) {
console.log('here!')
}
}
}
You are wanting an array of chips chosen, so you are missing multiple
from your template (is this just a typo when writing question?).您想要选择一系列芯片,因此您的模板中缺少multiple
芯片(这只是编写问题时的拼写错误吗?)。 As to your issue with old value, when the click
event happens, selectedFixtures
has yet to be updated with the value added/removed.关于旧值的问题,当click
事件发生时, selectedFixtures
尚未使用添加/删除的值进行更新。 You need to wait a tick to get the correct result:您需要等待一个勾号才能获得正确的结果:
Added multiple
to template:向模板添加了multiple
:
<v-chip-group v-model="selectedFixtures" multiple>
and adding waiting a tick in the function:并在函数中添加等待滴答声:
this.$nextTick(() => {
if (this.selectedFixtures.length === 1) {
console.log('here')
}
})
PS, as a recommendation, don't call functions in template when not needing to, I'm talking about filteredFixtures()
, you could instead use a computed property. PS,作为建议,不要在不需要时调用模板中的函数,我说的是filteredFixtures()
,您可以改用计算属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.