简体   繁体   English

在父级和子级Vue 2之间使用同步修改器

[英]Using sync modifier between Parent and Grandchildren Vue 2

Problem 问题

Let's say I have a vue component called: 假设我有一个名为vue的组件:

Note: All vue components has been simplified to explain what I'm trying to do. 注意:所有vue组件均已简化,以解释我要执行的操作。

reusable-comp.vue 可重复使用的,comp.vue

<template>
    <div class="input-group input-group-sm">
        <input type="text" :value.number="setValue" class="form-control" @input="$emit('update:setValue', $event.target.value)">
        <span>
            <button @click="incrementCounter()" :disabled="disabled" type="button" class="btn btn-outline-bordercolor btn-number" data-type="plus">
                <i class="fa fa-plus gray7"></i>
            </button>
        </span>
    </div>
</template>
<script>
    import 'font-awesome/css/font-awesome.css';

    export default {
        props: {
            setValue: {
                type: Number,
                required: false,
                default: 0
            }
        },
        data() {
            return {

            }
        },
        methods: {
            incrementCounter: function () {
                this.setValue += 1;
            }
        }
    }
</script>

Then in a parent component I do something like this: 然后在父组件中执行以下操作:

subform.vue subform.vue

<div class="row mb-1">
    <div class="col-md-6">
        Increment Value of Num A
    </div>
    <div class="col-md-6">
        <reuseable-comp :setValue.sync="numA"></reuseable-comp>
    </div>
</div>
<script>
    import reusableComp from '../reusable-comp'
    export default {
        components: {
            reusableComp 
        },
        props: {
            numA: {
                type: Number,
                required: false,
                default: 0
        }
    },
    data() {
        return {

        }
    }
</script>

then lastly 然后最后

page_layout.vue page_layout.vue

<template>
    <div>
        <subform :numA.sync="data1" />
   </div>
 </template>
 <script>
     import subform from '../subform.vue'
     export default {
         components: {
             subform
     },
     data() {
        return {
            data1: 0
        }
    }
 }
</script>

Question

So, how do I sync a value between reusable-comp.vue, subform.vue, and page_layout.vue 因此,如何在reusable-comp.vue,subform.vue和page_layout.vue之间同步值

I'm using reuseable-comp.vue is many different places. 我使用的reuseable-comp.vue有很多不同的地方。 I'm using subform.vue only a couple times in page_layout.vue 我在page_layout.vue中只使用了subform.vue几次

And I'm trying to use this pattern several times. 我试图多次使用此模式。 But I can't seem to get this to work. 但是我似乎无法使它正常工作。 The above gives me an error: 上面给我一个错误:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. 避免直接更改prop,因为每当父组件重新渲染时,该值都会被覆盖。 Instead, use a data or computed property based on the prop's value. 而是使用基于属性值的数据或计算属性。 Prop being mutated: "numA" 道具被突变:“ numA”

Okay I found a solution that worked. 好吧,我找到了一个可行的解决方案。

In subform.vue, we change: 在subform.vue中,我们更改:

    data() {
        return {
            numA_data : this.numA
        }
    }

So we now have reactive data to work with. 因此,我们现在可以使用反应性数据。 Then in the template, we refer to that reactive data instead of the prop: 然后在模板中,我们引用该反应性数据而不是prop:

<reuseable-comp :setValue.sync="numA_data"></reuseable-comp>

Then finally we add a watcher to check if the reactive data gets changed, and then emit to the parent: 最后,我们添加一个观察器,以检查反应性数据是否被更改,然后发送给父级:

watch: {
    numA_data: function(val) {
         this.$emit('update:numA', this.numA_data);
    }
}

Now all values from grandchildren to parent are synced. 现在,从孙辈到父母的所有值都已同步。


Update (4/13/2018) 更新(4/13/2018)

I made new changes to the reusable-comp.vue: 我对reusable-comp.vue进行了新更改:

  • I replaced where it says 'setValue' to 'value' 我将显示“ setValue”的位置替换为“ value”
  • I replaced where it says 'update:value' to 'input' 我将显示“ update:value”的地方替换为“ input”

Everything else says the same. 其他一切都一样。

Then in subform.vue: 然后在subform.vue中:

  • I replaced ':setValue.sync' to 'v-model' 我将':setValue.sync'替换为'v-model'

v-model is two way binding, so I made use of that where it needed to be. v模型是两种方式的绑定,因此我在需要的地方使用了它。 The sync between the parent-child (not child to grandchild), is still using sync modifier, only because the parent has many props to pass. 父子(不是子到子)之间的同步仍在使用sync修饰符,只是因为父有很多道具要传递。 I could modify this where I could group up the props as a single object, and just pass that. 我可以在将道具分组为单个对象的地方进行修改,并通过它。

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

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