composition-api and nuxt3 - I can't achieve responsiveness
P粉014293738
P粉014293738 2023-09-09 08:45:14
0
1
481

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粉014293738
P粉014293738

reply all(1)
P粉143640496

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template