在Vue.js 3中更新组合式 API 中的父级数值不起作用。

huangapple go评论73阅读模式
英文:

Updating parent values in Vue.js 3 doesn't work for composite API

问题

我有以下的Vue组件(基于PrimeVue):

<template lang="pug">
Dialog(:visible="dShow" :modal="true" :draggable="false" header="My Dialog" :style="{ width: '50vw' }" @update:visible="hide()")
  p test
  template(#footer)
    Button(label="Test")
</template>

<script setup>
import { ref, watch, defineEmits } from 'vue'

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

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  }
})

const dShow = ref(false)

watch(() => props.show, (newValue) => {
  dShow.value = newValue
})

const hide = () => {
  emit('update:show', false)
}

</script>

我的问题是,当我点击对话框的关闭图标时,会调用update:visible函数(hide),并发出update:show事件,但对话框不会消失。它仍然显示... 所以我认为父组件的值没有正确更新。你知道我在这里做错了什么吗?

我将该组件嵌入到父组件中,如下所示:

<template lang="pug">
Button(icon="icon pi pi-plus" @click="abcdef = true")
test(:show="abcdef")
</template>

<script setup>
import test from 'mydialog.vue'

const abcdef = ref(false)
</script>
英文:

I've the following vue component (based on PrimeVue):

&lt;template lang=&quot;pug&quot;&gt;
Dialog(:visible=&quot;dShow&quot; :modal=&quot;true&quot; :draggable=&quot;false&quot; header=&quot;My Dialog&quot; :style=&quot;{ width: &#39;50vw&#39; }&quot; @update:visible=&quot;hide()&quot;)
  p test
  template(#footer)
    Button(label=&quot;Test&quot;)
&lt;/template&gt;

&lt;script setup&gt;
import { ref, watch, defineEmits } from &#39;vue&#39;

const emit = defineEmits([&#39;update:show&#39;])

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  }
})

const dShow = ref(false)

watch(() =&gt; props.show, (newValue) =&gt; {
  dShow.value = newValue
})

const hide = () =&gt; {
  emit(&#39;update:show&#39;, false)
}

&lt;/script&gt;

My problem is when I click on the close icon of the dialog, the update:visible function (hide) is being called and the update:show event is getting emitted, but the dialog doesn't disappear. It's still show... so I assume the parent value isn't updated correctly. Any idea what I'm doing wrong here?

I embed the component in the parent as following:

&lt;template lang=&quot;pug&quot;&gt;
Button(icon=&quot;icon pi pi-plus&quot; @click=&quot;abcdef = true&quot;)
test(:show=&quot;abcdef&quot;)
&lt;/template&gt;

&lt;script setup&gt;
import test from &#39;mydialog.vue&#39;

const abcdef = ref(false)
&lt;/script&gt;

答案1

得分: 0

父组件目前设置了:show属性,但不会对更改作出反应。使用 v-model 来进行双向绑定,以便可以通过子组件通过 update:show 事件更新父组件中的值:

test(v-model:show="abcdef")
英文:

Currently, the parent component sets the :show prop, but it does not react to changes. Use v-model for two-way binding, so that the value in the parent component can be updated from the child through update:show events:

test(v-model:show=&quot;abcdef&quot;)

huangapple
  • 本文由 发表于 2023年7月27日 23:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定