Home  >  Q&A  >  body text

composition-api and nuxt3 - I can't achieve responsiveness

I have a working Nuxt code:

<template lang="pug">
div {{ isVisible }} !-- 响应性正常,isVisible从false切换到true --!
</template>
<script>
export default {


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

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

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

</script>

I tried using composition-api to convert, but it's not possible: it doesn't work.

I don't have an error message, but I feel unresponsive.

However, console.log will change the value (but the value in the template will not change)

<template lang="pug">
div {{ isVisible }} !-- 响应性不正常,当我点击时isVisible始终为false(但是通过console.log,值会改变) --!
</template>
<script setup>
const { locale, locales } = useI18n()
const switchLocalePath = useSwitchLocalePath()

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

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

</script>

P粉014293738P粉014293738406 days ago461

reply all(1)I'll reply

  • P粉143640496

    P粉1436404962023-09-10 07:17:06

    You need to change the following lines

    isVisible = !isVisible;

    change to

    isVisible.value = !isVisible.value

    For more information, please refer to: https://vuejs.org/guide/essentials/reactivity-fundamentals.html#ref

    reply
    0
  • Cancelreply