Vue 3的class属性似乎忽略了ref。

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

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.

&lt;script setup lang=&quot;ts&quot;&gt;
import { ref, watch } from &quot;vue&quot;;
import { useColors } from &quot;vuestic-ui&quot;;

const { currentPresetName } = useColors();

const isDarkMode = ref();

watch(currentPresetName, () =&gt; {
  isDarkMode.value = currentPresetName.value === &#39;dark&#39;;
})
&lt;/script&gt;
&lt;template&gt;
  &lt;div :class=&quot;[&#39;homeBackground&#39;, isDarkMode ? &#39;backgroundDark&#39; : &#39;&#39;]&quot; v-once&gt;&lt;/div&gt;
&lt;/template&gt;
&lt;style lang=&quot;scss&quot; &gt;
.homeBackground {
  height: 93vh;
  background-image: url(&#39;@/assets/LargeLogo.png&#39;);
  background-repeat: no-repeat;
  background-position: center;
  background-size: 40vw;
  background-color: unset !important;
}

.backgroundDark {
  background-image: url(&#39;@/assets/LargeLogo-Dark.png&#39;);

}
&lt;/style&gt;

答案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:

&lt;div class=&quot;homeBackground&quot; :class=&quot;{backgroundDark: currentPresetName === &#39;dark&#39;}&quot;&gt;&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年7月3日 23:24:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定