composition-api 和 nuxt3 – 我没有响应性

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

composition-api and nuxt3 - i don’t have reactivity

问题

  1. <template lang="pug">
  2. div {{ isVisible }} !-- 响应性 OK,isVisible 从 false 切换到 true --!
  3. </template>
  1. <template lang="pug">
  2. div {{ isVisible }} !-- 响应性不行,当我点击时 isVisible 始终为 false(但是通过 console.log,值会改变) --!
  3. </template>
英文:

I have a nuxt code that works :

  1. &lt;template lang=&quot;pug&quot;&gt;
  2. div {{ isVisible }} !-- Reactivity OK, isVisible switch from false to true --!
  3. &lt;/template&gt;
  4. &lt;script&gt;
  5. export default {
  6. data() {
  7. return {
  8. isVisible: false
  9. }
  10. },
  11. computed: {
  12. availableLocales() {
  13. return this.$i18n.locales.filter(i =&gt; i.code !== this.$i18n.locale)
  14. }
  15. },
  16. methods: {
  17. showDropdown() {
  18. console.log(this.isVisible);
  19. this.isVisible = !this.isVisible;
  20. }
  21. }
  22. }
  23. &lt;/script&gt;

I try to convert it using composition-api but impossible: it doesn’t work.
I don’t have an error message but I feel like I don’t have responsiveness.
However, the console.log changes the value (but the value is not changed in the template)

  1. &lt;template lang=&quot;pug&quot;&gt;
  2. div {{ isVisible }} !-- Reactivity NOK, isVisible is always false when I click (but with a console.log, the value change) --!
  3. &lt;/template&gt;
  4. &lt;script setup&gt;
  5. const { locale, locales } = useI18n()
  6. const switchLocalePath = useSwitchLocalePath()
  7. const availableLocales = computed(() =&gt; {
  8. return (locales.value).filter(i =&gt; i.code !== locale.value)
  9. });
  10. let isVisible = ref(false);
  11. const showDropdown = () =&gt; {
  12. console.log(isVisible);
  13. isVisible = !isVisible;
  14. }
  15. &lt;/script&gt;

答案1

得分: 1

你需要更改该行的代码:

  1. isVisible = !isVisible;

为:

  1. isVisible.value = !isVisible.value;
英文:

You need to change the line

  1. isVisible = !isVisible;

to

  1. isVisible.value = !isVisible.value

more info here: https://vuejs.org/guide/essentials/reactivity-fundamentals.html#ref

huangapple
  • 本文由 发表于 2023年6月15日 21:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482806.html
匿名

发表评论

匿名网友

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

确定