简体   繁体   English

如何防止Vue.js中循环组件的数据/方法共享

[英]How to prevent the data/method sharing of looped components in Vue.js

I have the vue component with $emit into component and let it return the data from the component. 我将带有$ emit的vue组件放入组件中,并使其从该组件返回数据。 I will use the component to update current page's data. 我将使用该组件来更新当前页面的数据。 the codes below 下面的代码

Template: 模板:

<Testing
@update="update">
</Testing>
<AnotherComponent
:data="text"
>
</AnotherComponent>

Script: 脚本:

method(){
    update: function(data){
        this.text = data.text
    }
}

it work perfectly if only this one. 如果只有这个,它会完美地工作。

Now , i need to make a button to add one more component. 现在,我需要创建一个按钮来添加更多组件。 I use the for loop to perform this. 我使用for循环执行此操作。

Template 模板

<div v-for="index in this.list">
    <Testing
    :name="index"
    @update="update">
    </Testing>
    <AnotherComponent
    :data="text"
    >
    </AnotherComponent>
</div>

Script: 脚本:

method(){
    addList : function(){
        this.list +=1;
    },
    deleteList : function(){
        this.list -=1;
    },
    update: function(data){
        this.text = data.text
    }
}

The add and delete function run perfectly. 添加和删​​除功能运行完美。 However , they share the "update" method and the "text" data. 但是,它们共享“更新”方法和“文本”数据。 so , If I change the second component , the first component will also changed. 因此,如果我更改第二个组件,则第一个组件也会更改。 I think this is not the good idea to copy the component. 我认为这不是复制组件的好主意。

Here are my requirements. 这是我的要求。

  1. This component is the part of the form, so they should have different name for submit the form. 该组件是表单的一部分,因此提交表单时应使用不同的名称。

  2. The another component" will use the data from the "testing component" to do something. the "testing" and "another component" should be grouped and the will not change any data of another group. “另一个组件”将使用“测试组件”中的数据进行操作。“测试”和“另一个组件”应进行分组,并且不会更改另一个组的任何数据。

Any one can give me the suggestion how to improve these code? 任何人都可以给我建议如何改进这些代码? Thanks 谢谢

What happends is that both are using the data form the parent, and updating that same data. 发生的事情是两者都使用父级的数据,并更新了相同的数据。

It seems that you are making some kind of custom inputs. 看来您正在进行某种自定义输入。 In that case in your child component you can use 'value' prop, and 'input' event, and in the parent user v-model to keep track of that especific data data. 在这种情况下,您可以在子组件中使用“值”属性和“输入”事件,并在父用户v模型中使用该特定数据数据。

Child component BaseInput.vue: 子组件BaseInput.vue:

<template>
  <div>
    <input type="text" :value="value" @keyup="inputChanged">
  </div>
</template>

<script>
export default {
  props: ['value'],
  data () {
    return {

    }
  },
  methods: {
    inputChanged (e) {
      this.$emit('input', e.target.value)
    }
  }
}
</script>

And this is the code on the parent: 这是父代码:

<template>
<div class="container">
    <div class="row">
        <div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
            <base-input v-model="firstInputData"></base-input>
            <p>{{ firstInputData }}</p>
            <hr>
            <base-input v-model="secondInputData"></base-input>
            <p>{{ secondInputData }}</p>

        </div>
    </div>
</div>

<script>
import BaseInput from './BaseInput.vue'
    export default {
        components: {BaseInput},
        data() {
            return{
                firstInputData: 'You can even prepopulate your custom inputs',
                secondInputData: ''
            }
        }
    }
</script>

In the parent you could really store the diferent models in an object as properties, and pass that object to that "The another component" , pass them as individual props... pass an array .... 在父级中,您实际上可以将不同的模型作为属性存储在一个对象中,然后将该对象传递给该“另一个组件”,将它们作为单独的props传递...传递一个数组...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM