我正在try 为页面上的每一项创建按钮和VUE元素输入.我正在迭代这些项,并用v-for来呈现它们,所以我决定对此进行扩展,并对其余两个项都这样做.我遇到的问题是我需要分别绑定textInputdisplayTextbox,但我不确定如何实现这一点.

目前,el-ins中的所有输入文本都绑定到同一个变量,单击以显示输入将同时显示所有输入.

<template>
    <div class="container">
        <div v-for="(item, index) in items" :key="index">
            <icon @click="showTextbox"/>
            <el-input v-if="displayTextbox" v-model="textInput" />
            <el-button v-if="displayTextbox" type="primary" @click="confirm" />
            <ItemDisplay :data-id="item.id" />
        </div>
    </div>
</template>

<script>
import ItemDisplay from '@/components/ItemDisplay';

export default {
    name: 'ItemList',

    components: {
        ItemDisplay,
    },

    props: {
        items: {
            type: Array,
            required: true,
        },
    }

    data() {
        displayTextbox = false,
        textInput = '',
    },

    methods: {
        confirm() {
            // todo send request here
            this.displayTextbox = false;
        },

        showTextbox() {
            this.displayTextbox = true;
        }
    }
}
</script>

编辑:在@kissu的帮助下,以下是更新后的工作版本

<template>
    <div class="container">
        <div v-for="(item, index) in itemDataList" :key="itemDataList.id">
            <icon @click="showTextbox(item.id)"/>
            <El-Input v-if="item.displayTextbox" v-model="item.textInput" />
            <El-Button v-if="item.displayTextbox" type="primary" @click="confirm(item.id)" />
            <ItemDisplay :data-id="item.item.uuid" />
        </div>
    </div>
</template>

<script>
import ItemDisplay from '@/components/ItemDisplay';

export default {
    name: 'ItemList',

    components: {
        ItemDisplay,
    },

    props: {
        items: {
            type: Array,
            required: true,
        },
    }

    data() {
        itemDataList = [],
    },

    methods: {
        confirm(id) {
            const selected = this.itemDataList.find(
                (item) => item.id === id,
            )
            selected.displayTextbox = false;
            console.log(selected.textInput);
            // todo send request here
        },

        showTextbox(id) {
            this.itemDataList.find(
                (item) => item.id === id,
            ).displayTextbox = true;
        },
        
        populateItemData() {
            this.items.forEach((item, index) => {
                this.itemDataList.push({
                    id: item.uuid + index,
                    displayTextbox: false,
                    textInput: '',
                    item: item,
                });
            });
        }
    },

    created() {
        // items prop is obtained from parent component vuex
        // generate itemDataList before DOM is rendered so we can render it correctly
        this.populateItemData();
    },
}
</script>

推荐答案

[assuming you're using Vue2]
If you want to interact with multiple displayTextbox + textInput state, you will need to have an array of objects with a specific key tied to each one of them like in this example.
As of right now, you do have only 1 state for them all, meaning that as you can see: you can toggle it for all or none only.

您需要使用我上面的示例中的对象对其进行重构,以允许在每个状态上逐个迭代.

PS: :key="index" is not a valid solution, you should never use the index of a v-for as explained here.
PS2: please follow the conventions in terms of component naming in your template.


Also, I'm not sure how deep you were planning to go with your components since we don't know the internals of <ItemDisplay :data-id="item.id" />.
But if you also want to manage the labels for each of your inputs, you can do that with nanoid, that way you will be able to have unique UUIDs for each one of your inputs, quite useful.

Vue.js相关问答推荐

Vue CDN的html工作,但v—如果不工作

如何用其他组件编译Vue模板?

视图中的 vue 样式不适用于 html

在 onMounted 之前渲染的 Vue 模板

Vue 3 如何通过多个组件传递插槽

我如何使用 vuejs 在我的 url 中编码一个令牌

如何在 vue js 的一页路由下显示不同的 category._id

vite - 具有相对assets资源 路径的子页面

Vuejs - 更新数组中对象的数组

webpack vue-loader 配置

如果 Vuetify 自动完成没有匹配的结果,则保留输入的文本

如何将参数传递给使用 ...mapActions(...) 映射的函数?

Nuxt:显示静态文件夹中的本map像

Vue-test-utils:在单个测试中多次使用 $nextTick

如何在 NPM 上创建和发布 Vuejs 组件

如何在 Vue.js 中获取组件元素的 offsetHeight?

Vue.js:TypeError:无法设置只有 getter 的 # 的属性props

取消选中单选按钮

带有 Firebase 云消息传递的 Vue pwa 无法正常工作

如何在我的 vue js 中实现无限滚动