javascript - console.log() 不显示实际值
问题描述
我正在遍历一个列表,在该列表上我使用了 @click 函数来更新一些其他元素。元素索引值绑定到selectedFixtures
. 我不明白为什么,当我点击一个元素时,它会打印出来[]
,而我第二次点击一个元素时,它会打印出旧值 ex.: [1]
。如何使用我的函数中的实际值以便它[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!')
}
}
}
解决方案
您想要选择一系列芯片,因此您multiple
的模板中缺少(这只是写问题时的错字吗?)。至于您的旧值问题,当click
事件发生时,selectedFixtures
尚未使用添加/删除的值进行更新。您需要稍等一下才能得到正确的结果:
添加multiple
到模板:
<v-chip-group v-model="selectedFixtures" multiple>
并在函数中添加等待滴答声:
this.$nextTick(() => {
if (this.selectedFixtures.length === 1) {
console.log('here')
}
})
PS,作为建议,不要在不需要时调用模板中的函数,我说的是 filteredFixtures()
,您可以改用计算属性。
推荐阅读
- asp.net-core - 带有 IdentityServer4 的 Asp.net Core 2 - cookie 过期后重定向到登录
- html - 如何在数组 angular 6 中找到最接近的较低值?
- java - 如何使用 java 从 sap 获取宇宙、类、对象、报告名称、报告字段
- javascript - 我无法解析多行 json 数组并将其放入 html 元素中
- ruby-on-rails - 如何在 ruby on rails 的后台每 5 秒定期轮询/查询数据库
- phonegap-build - 更新 Android PhoneGap 应用程序的版本代码
- html - 无法向下滚动超过某个点 - iPhone X 兼容性问题
- html - 按钮href忽略问号后的所有内容
- javascript - 如何使用 javascript 获取 api 以显示数据?
- c# - 错误 CS0619:`UnityEngine.RuntimePlatform.WindowsWebPlayer'