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>
You need to change the following lines
change to
For more information, please refer to: https://vuejs.org/guide/essentials/reactivity-fundamentals.html#ref