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

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

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

问题

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

I have a nuxt code that works :

&lt;template lang=&quot;pug&quot;&gt;
div {{ isVisible }} !-- Reactivity OK, isVisible switch from false to true --!
&lt;/template&gt;
&lt;script&gt;
export default {


  data() {
    return {
      isVisible: false
    }
  },

  computed: {
    availableLocales() {
      return this.$i18n.locales.filter(i =&gt; i.code !== this.$i18n.locale)
    }
  },

  methods: {
    showDropdown() {
      console.log(this.isVisible);
      this.isVisible = !this.isVisible;
    }
  }
}

&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)

&lt;template lang=&quot;pug&quot;&gt;
div {{ isVisible }} !-- Reactivity NOK, isVisible is always false when I click (but with a console.log, the value change) --!
&lt;/template&gt;
&lt;script setup&gt;
const { locale, locales } = useI18n()
const switchLocalePath = useSwitchLocalePath()

const availableLocales = computed(() =&gt; {
  return (locales.value).filter(i =&gt; i.code !== locale.value)
});

let isVisible = ref(false);
const showDropdown = () =&gt; {
  console.log(isVisible);
  isVisible = !isVisible;
}

&lt;/script&gt;

答案1

得分: 1

你需要更改该行的代码:

isVisible = !isVisible;

为:

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

You need to change the line

isVisible = !isVisible;

to

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:

确定