简体   繁体   English

子发射不更新父级的 v-model prop 数组

[英]Child emit does not update v-model prop Array of parent

I'm using this parent component:我正在使用这个父组件:

<template>
  <div id="cms-custom-editor" class="cms-custom-editor">
    <cms-editor-toolbar v-model:toggles="state.toggles"/>
    <div class="content">Toggle Buttons: {{ state.toggles }}</div>
  </div>
</template>


<script setup>
import CmsEditorToolbar from './cms-editor-toolbar.vue'
import {reactive, ref} from "vue";

const state = reactive({
    toggles: [],
})
</script>

to pass down state.toggles as a prop to the cms-editor-toolbar child component:state.toggles作为道具传递给cms-editor-toolbar子组件:

<template>
  <div id="cms-editor-toolbar" class="cms-editor-toolbar">
    <button @click="toggleButton"></button>
  </div>
</template>


<script setup>

const props = defineProps({
    toggles: Array,
})

const emit = defineEmits(['update:toggles'])

const toggleButton = () => {
    // ... logic to determine toggleAction
    console.log(toggleAction) // logs correct toggleAction
    emit('update:toggles', toggleAction) //emits only first toggleAction and no other
}

</script>

For whatever reason emit('update:toggles', toggleAction) only emits the first toggleAction to the parent.无论出于何种原因emit('update:toggles', toggleAction)只向父级发出第一个toggleAction For every other change, the state.toggles array is not updated.对于其他所有更改, state.toggles数组都不会更新。

From the answer in this question , I've tried using ref instead of reactive :这个问题的答案中,我尝试使用ref而不是reactive

<template>
  <div id="cms-custom-editor" class="cms-custom-editor">
    <cms-editor-toolbar v-model:toggles="toggles"/>
    <div class="content">Toggle Buttons: {{ toggles }}</div>
  </div>
</template>


<script setup>
import CmsEditorToolbar from './cms-editor-toolbar.vue'
import {reactive, ref} from "vue";

const toggles = ref([1,2,3])
</script>

but it changes nothing.但它什么也没改变。 Again the array is only updated once and no consecutive action has any effect.同样,数组只更新一次,没有连续的动作有任何影响。 How do I make this work?我该如何进行这项工作?

Edit:编辑:

I've added additional props (not arrays) to sibling elements inside the child, which not only are emitted correctly, but also trigger all actions of the array emit .我已经为子元素内的兄弟元素添加了额外的道具(不是数组),它们不仅被正确发射,而且还触发了array emit Basically it seems to save the array inside the child until the emit is triggered by a sibling emit, which sends the complete array to the parent.基本上,它似乎将数组保存在子级中,直到由兄弟级发射触发发射,它将完整的数组发送给父级。 No idea how this is happening.不知道这是怎么回事。

I've been using the wrong method to check if state.toggles is updated.我一直在使用错误的方法来检查state.toggles是否已更新。 Rendering it in a paragraph usually works, but in this case it doesn't seem to be rerendered for whatever reason.在一个段落中渲染它通常是可行的,但在这种情况下,它似乎并没有因为任何原因被重新渲染。

Logging the array inside the parent like so:像这样在父级中记录数组:

setInterval(function(){
    console.log(state.toggles)
}, 1000);

shows that it is indeed updating.表明它确实在更新。 The emits are working.发射器正在工作。

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

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