监听Vue中的Ref<string>变化,即使值更改为0,仍然保留旧值

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

Listen to Ref<string> change in Vue even if value is changed to 0 then the old one

问题

我遇到了Vue3的watchonUpdated的问题:

我可以正常使用这两个来触发回调,但是让我举个例子:

  1. 用户点击删除按钮,并将targetId设置为someId
  2. &lt;Modal/&gt;watch被触发,因为props.id已更改,因此引导一个模态框出现
  3. 用户误触模态框外部导致模态框隐藏。
  4. 用户再次点击删除按钮,但这次targetId已经是someId,所以不会触发&lt;Modal/&gt;watch。没有模态框显示出来

你可以在我的代码中看到,我尝试在每次调用showModal时将targetId设置为0,但没有帮助。在我看来,targetId确实改变了两次,但不知道为什么&lt;Modal/&gt;没有"监视"它。

基本上我想要在旧值等于新值时触发watchonUpdated(<u>我知道直接做不可能</u>),所以我尝试将它设置为默认值,然后再改回来,希望触发"旧值 -> 0"和"0 -> 旧值"两次更改,但没有帮助。


&lt;!-- 父组件 --&gt;
&lt;template&gt;
  &lt;button @onclick=&quot;showDeleteModal(someId)&quot;&gt;删除&lt;/buton&gt;
  &lt;Modal :id=&quot;targetId&quot;/&gt;
&lt;/template&gt;

&lt;script setup&gt;
   const targetId = ref(0)
   const showDeleteModal = (id)=&gt;{
      &lt;!-- 将其更改为0然后再更改为id没有用 --&gt;
      targetId.value = 0
      targetId.value = id
   }
&lt;/script&gt;



&lt;!-- 子组件(Modal) --&gt;
&lt;template&gt;
   一些模板
&lt;/template&gt;

&lt;script setup&gt;
   const props = defineProps({id: Number})
   watch(()=&gt;props.id, ()=&gt;{
      &lt;!-- 显示模态框 --&gt;
   })
&lt;/script&gt;

英文:

I got into a problem with Vue3's watch or onUpdated:

I can trigger the callback normally with both, but let's I got a senario below:

  1. A user click Delete button, and set the targetId to someId
  2. &lt;Modal/&gt;'s watch was triggered because props.id was changed, so a bootstrap Modal show up
  3. User misclicked on outside the modal and cause the modal to hide.
  4. User click Delete button again, but this time, targetId was already someId, so &lt;Modal/&gt;'s watch will not be triggered. And no modal will shown up

You can see in my code I tried to set targetId to 0 everytime showModal was called, but no help. In my opinion, targetId did change, twice, but don't know why it's not "watched" by &lt;Modal/&gt;

Basically I want to trigger watch or onUpdated even if old value is equel to the new value(<u>I know this is not possiable in a direct way</u>, so I tried to set it to a default value, then change it back, hoping to trigger "oldVal -> 0" and "0 -> oldVal" two changes, but no help.)


&lt;!-- Parent --&gt;
&lt;template&gt;
  &lt;button @onclick=&quot;showDeleteModal(someId)&quot;&gt;Delete&lt;/buton&gt;
  &lt;Modal :id=&quot;targetId&quot;/&gt;
&lt;/template&gt;

&lt;script setup&gt;
   const targetId = ref(0)
   const showDeleteModal = (id)=&gt;{
      &lt;!-- change to 0 then change back to id does not work --&gt;
      targetId.value = 0
      targetId.value = id
   }
&lt;/script&gt;



&lt;!-- Child(Modal) --&gt;
&lt;template&gt;
   some template
&lt;/template&gt;

&lt;script setup&gt;
   const props = defineProps({id: Number})
   watch(()=&gt;props.id, ()=&gt;{
      &lt;!-- Show the Modal --&gt;
   })
&lt;/script&gt;

答案1

得分: 1

更改在同一函数中来回变动的值不起作用,因为更改检测仅在函数结束后触发。您可以使用 nextTick()

import { nextTick } from 'vue'
const showDeleteModal = (id) => {
  targetId.value = 0
  nextTick(() => targetId.value = id)
}

然而,这会触发两次模态框,并且这是一个笨拙的解决方法。

从您的描述中,似乎在模态框关闭时删除 id 可能是一个选择。如果可以的话,我会选择这个选项。

英文:

Changing the value back and forth in the same function doesn't work, because change detection will only trigger after the function has concluded. You could use nextTick():

import { nextTick } from &#39;vue&#39;
const showDeleteModal = (id)=&gt;{
  targetId.value = 0
  nextTick(() =&gt; targetId.value = id)
}

However, this will trigger the modal twice, and it is an awkward solution.

From your description, it sounds like it might be possible to remove the id when the modal closes. If that is an option, I would go with that.

huangapple
  • 本文由 发表于 2023年4月19日 18:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053304.html
匿名

发表评论

匿名网友

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

确定