英文:
Vue 3 class attribute appears to ignore ref
问题
我很迷失。当currentPresetName
改变时,watch
会触发,而isDarkMode
也会更新。出于某种原因,似乎:class
完全忽略了我的引用。它在初始加载时检查它,然后再也不会有反应。
<script setup lang="ts">
import { ref, watch } from "vue";
import { useColors } from "vuestic-ui";
const { currentPresetName } = useColors();
const isDarkMode = ref();
watch(currentPresetName, () => {
isDarkMode.value = currentPresetName.value === 'dark';
})
</script>
<template>
<div :class="['homeBackground', isDarkMode ? 'backgroundDark' : '']" v-once></div>
</template>
<style lang="scss" >
.homeBackground {
height: 93vh;
background-image: url('@/assets/LargeLogo.png');
background-repeat: no-repeat;
background-position: center;
background-size: 40vw;
background-color: unset !important;
}
.backgroundDark {
background-image: url('@/assets/LargeLogo-Dark.png');
}
</style>
英文:
I'm super lost. The watch is getting hit when the currentPresetName changes and isDarkMode is getting updated. For whatever reason, it seems as though :class
is completely ignoring my ref. It checks it on the initial load and then never reacts.
<script setup lang="ts">
import { ref, watch } from "vue";
import { useColors } from "vuestic-ui";
const { currentPresetName } = useColors();
const isDarkMode = ref();
watch(currentPresetName, () => {
isDarkMode.value = currentPresetName.value === 'dark';
})
</script>
<template>
<div :class="['homeBackground', isDarkMode ? 'backgroundDark' : '']" v-once></div>
</template>
<style lang="scss" >
.homeBackground {
height: 93vh;
background-image: url('@/assets/LargeLogo.png');
background-repeat: no-repeat;
background-position: center;
background-size: 40vw;
background-color: unset !important;
}
.backgroundDark {
background-image: url('@/assets/LargeLogo-Dark.png');
}
</style>
答案1
得分: 3
移除 v-once
,它将按预期工作。这个“bug”实际上是一个特性。这就是 v-once
的作用:
在后续重新渲染时,元素/组件及其所有子元素都将被视为静态内容并被跳过。这可以用于优化更新性能。
英文:
Remove v-once
and it will work as expected. The "bug" is a feature. It's what v-once
does:
> On subsequent re-renders, the element/component and all its children will be treated as static content and skipped. This can be used to optimize update performance.
答案2
得分: 0
首先,你不需要一个中间的引用变量 isDarkMode
。不要用只在下一行使用且只用几个字初始化的变量来使代码混乱。
其次,只有当你改变数据时才使用 watch
,对于派生的计算值,比如 isDarkMode
,请使用 computed
。
第三,将你的静态 homeBackground
类移到类的静态属性,这样就能明确哪些类是静态的,哪些是动态的。
最后但最重要的一点:
尝试移除 v-once
,因为在元素首次渲染之后,currentPresetName
可能会改变为所需的 dark
值。
模板中的 JS 表达式是 computed
的,所以尝试使用:
<div class="homeBackground" :class="{backgroundDark: currentPresetName === 'dark'}"></div>
英文:
First you don't need an intermediate ref isDarkMode
. Like don't clutter code with variables that used only once on the next line and initialized with a couple of words.
Second use watch
only if you mutate data, for derived calculated values like isDarkMode
use computed
.
Third move your static homeBackground
class to class
static attribute so it would be obvious which classes are static and which ones are dynamic.
And the last but the most important:
try to remove v-once
since currentPresetName
could change to the needed dark
value after the first rendering of the element.
JS expressions inside a template are computed
so try:
<div class="homeBackground" :class="{backgroundDark: currentPresetName === 'dark'}"></div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论