UPDATE 能够使其正常工作,但遇到了最后一个问题。更新的代码在这里: VueJ不适用于首次点击或首次事件
我一直在尝试寻找一种使循环内的组件不起作用的方法。
我有一个循环(3格),在循环内,我有2个文本框。但是,每当我在其中任何一个中输入值时,该值就会填充到所有人。
谁能帮我分离这些组件?
我正在尝试使父div(第一循环)动态化。因此,子组件(第二个循环)应该与自己的祖父母组件(文本框)分开工作。
这是我的代码:
<div id="app">
<div v-for="(ctr, c) in 3" :key="c">
<button @click="input_add">1st</button>
<div>
<div v-for="(input, act) in inputs" :key="act.id">
<input type="text" v-model="input.name">
<input type="text" v-model="input.time">
<button @click="input_remove(act)">Delete</button>
<button @click="input_add">Add row</button>
</div>
</div>
{{ inputs }}
</div>
</div>
const app = new Vue({
el: "#app",
data: {
inputs: [],
counter: 0,
},
methods: {
input_add() {
this.inputs.push({
id: this.counter + 1,
day: null,
name: null,
time: null,
})
this.counter += 1
},
input_remove(index) {
this.inputs.splice(index,1)
this.counter -= 1
}
}
});
结果:
正如我在评论中提到的那样,您应该为迭代项创建一个组件。
父组件:
<div v-for="(item, index) in array" :key="index">
<child :item="item" />
</div>
现在,您发送了item
as道具。让我们在孩子中抓住它。
子组件:
<div>
<input type="text" v-model="input.name">
<input type="text" v-model="input.time">
<button @click="input_remove(act)">Delete</button>
<button @click="input_add">Add row</button>
</div>
{{ inputs }}
props: [item], // I am not sure you need it or not, BUT just showing how to do it.
data() {return { // your datas };},
methods: {
// your methods...
},
//and else...
现在,每个迭代项都只能控制自身。我希望现在有道理。
然后在子组件中将按钮构建为输入。之后,您可以将事件应用于仅单击的项目。
抱歉,您可以进一步解释吗?我不熟悉<child>
首先创建一个组件,然后将其导入父组件。然后将其用作
div
您应用的子组件v-for
。现在,像我在答案中提到的那样发送道具。然后把道具拿到孩子身上。用它。我现在明白了。无需将所有内容都包含在循环中,只需创建父级的数据,然后分别循环子级。谢谢!!
没问题。如果它解决了您的问题,请检查它是否为其他人的答案。@布莱
嘿,泰。我能够使它工作。我只需要在迭代之前准备好数据即可。但是我还有另一个问题,如果您不忙于检查它,这里是:stackoverflow.com/questions/60077788/…