gpt4 book ai didi

javascript - 在 Vuex 状态下对数组使用 v-model 和 v-for

转载 作者:搜寻专家 更新时间:2023-10-30 22:25:06 25 4
gpt4 key购买 nike

我正在为选项卡式界面构建选项卡,用户可以在其中更改选项卡的标题。我想做的是这样的:

标记:

<div class="tabs" v-for="(tab, tabIndex) in tabs" :key="tab.id" >
<input type="text" v-model:value="tabs(tabIndex)">
</div>

计算属性:

computed: {
scenes2: {
get(tabIndex){
return this.$store.state.tabs[tabIndex].title
},
set(value, tabIndex){
this.$store.dispatch('setTabTitle', {value: value, tabIndex: tabIndex} )
},
},
},

这当然行不通。在这里使用 v-model 的正确方法是什么,以便我可以将 v-model 与 Vuex 状态下 tabs 数组上的相关索引相关联?

最佳答案

我不会对数组和对象使用计算属性。

 <div class="tabs" v-for="(tab, tabIndex) in $store.state.tabs" :key="tab.id">
<input type="text" :value="tab.title" @input="e => updateTabTitle(tabIndex, e)">
</div>

methods: {
updateTabTitle(tabIndex, event) {
this.$store.dispatch('setTabTitle', {value: event.target.value, tabIndex} )
}
}

关于javascript - 在 Vuex 状态下对数组使用 v-model 和 v-for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49967632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com