admin管理员组文章数量:1287859
I'm passing a prop to a ponent declaring in which state it is. I want to watch the prop and set the css accordingly. But it does not work, can someone telling me what I'm doing wrong?
<script setup>
import { onMounted, reactive, ref, watch, watchEffect } from 'vue'
const props = defineProps({
title: String,
date: String,
state: String,
})
let cardStatus = ref('')
watch(props.state, () => {
if (props.state === 'finished'){
cardStatus.value = 'border border-success'
}
})
</script>
<template>
<div :class="'bg-main-light rounded-2xl w-full p-3 lg:px-5 ' + cardStatus"></div>
</template>
I'm passing a prop to a ponent declaring in which state it is. I want to watch the prop and set the css accordingly. But it does not work, can someone telling me what I'm doing wrong?
<script setup>
import { onMounted, reactive, ref, watch, watchEffect } from 'vue'
const props = defineProps({
title: String,
date: String,
state: String,
})
let cardStatus = ref('')
watch(props.state, () => {
if (props.state === 'finished'){
cardStatus.value = 'border border-success'
}
})
</script>
<template>
<div :class="'bg-main-light rounded-2xl w-full p-3 lg:px-5 ' + cardStatus"></div>
</template>
Share
Improve this question
edited Jun 17, 2022 at 22:25
Nikola Pavicevic
23.5k9 gold badges29 silver badges51 bronze badges
asked Jun 17, 2022 at 14:32
Lx45Lx45
1551 gold badge3 silver badges12 bronze badges
3 Answers
Reset to default 10try like following:
watch(
() => props.state,
(newValue, oldValue) => {
if (newValue === 'finished') cardStatus.value = 'border border-success'
}
);
I found a way to make it work.
<script setup>
import { onMounted, reactive, ref, watch, watchEffect } from 'vue'
const props = defineProps({
title: String,
date: String,
state: String,
})
let cardStatusClass = ref('')
watchEffect(() => {
if (props.state === 'finished'){
cardStatusClass.value = 'border border-success'
}
})
</script>
I found this way
const retWatch: any = toRef(props, 'props.state');
watch([retWatch], (newValue) => {
console.log(newValue);
});
本文标签: javascriptUsing Watchers on props in vue3 Script SetupStack Overflow
版权声明:本文标题:javascript - Using Watchers on props in vue3 Script Setup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741309712a2371580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论