[英]How can I use a computed property to change the color of a text pill depending on the word using vue composition api?
I have a pill shape that say's either Production or Development.我有一个药丸形状,上面写着生产或开发。 I want the background color to be different depending on if it is production or development.
我希望背景颜色根据是生产还是开发而不同。 I've done this before with option api, but I'm having trouble in composition api.
我以前用选项 api 做过这个,但我在组合 api 时遇到了麻烦。
Computed Method:计算方法:
const reportStatus = computed(function() {
if (dashboard.status === 'Production') {return 'status--production'}
return 'status--qa'
});
Styling:造型:
.status--production {
background-color: blue;
color: white;
}
.status--qa {
background-color: green;
color: white;
}
Template code:模板代码:
<div>
<span class="float-left text-center w-24 inline-block flex-shrink-0 rounded-full px-2 py-0.5 text-xs font-medium text-white inset-x-0 top-0" :class="reportStatus">
{{ dashboards.status }}</span>
</div>
Script Dashboard code:脚本仪表板代码:
const dashboard = [
{
report: "Ad Hoc",
description: "",
status: "Production"
},
Use computed properties for things like changing reactive variable output. For example, some different date format.使用计算属性来更改反应变量 output。例如,一些不同的日期格式。
The best way to your issue is:解决您的问题的最佳方法是:
<template>
<span class="fload ..." :class="[ dashboard.status === 'Production' ? 'status-production' : 'status-qa']">{{ dashboard.status }}</span>
</template>
Make sure dashboard is a reactive object/value like ref()
or reactive()
second one fit best for objects.确保仪表板是反应性对象/值,如
ref()
或reactive()
第二个最适合对象。 Objects are tricky to use every time you have to assign a full new object instead of just change a value in it.每次您必须分配一个全新的 object 而不是仅仅更改其中的值时,对象很难使用。
Computed property:计算属性:
<template>
<div>
<span class="float ..." :class="addClass">{{ dashboard.status }}</span>
<button @click="dashboard = { ...dashboard, status: 'lol' }">Change</button>
<button @click="dashboard = { ...dashboard, status: 'Production' }">Production</button>
</div>
</template>
<script setup>
const dashboard = ref({ status: 'Production' })
const addClass = computed(() => dashboard.value.status === 'Production' ? 'status-production' : 'status-qa')
</script>
If you use ref()
and change dashboard like "dashboard.value.status = 'some value'" reactivity won't work same as "dashboard.status = 'some value'" in template.如果您使用
ref()
并更改仪表板,如“dashboard.value.status = 'some value'”,反应性将与模板中的“dashboard.status = 'some value'”不同。 You will always need to assign a new object to trigger reactivity.您将始终需要分配一个新的 object 来触发反应。
reactive()
don't have that problem because all items inside of it are reactive. reactive()
没有这个问题,因为它里面的所有项目都是反应性的。
When you have two class attributes, the second one gets ignored.当您有两个 class 属性时,第二个会被忽略。 Combine your two class attributes into one;
将您的两个 class 属性合二为一;
<div>
<span :class="`float-left text-center w-24 inline-block flex-shrink-0 rounded-full px-2 py-0.5 text-xs font-medium text-white inset-x-0 top-0 ${reportStatus}`">
{{ dashboards.status }}
</span>
</div>
Your code should work fine, Just for a demo purpose I am using just an object for dashbaords
variable but you can change it to an array as per your requirement.您的代码应该可以正常工作,仅出于演示目的,我仅将 object 用于
dashbaords
变量,但您可以根据需要将其更改为数组。
Here is the live demo (Please have a look and try to find the root cause of the issue you are facing by referring this) :这是现场演示(请看一下并尝试通过参考找到您所面临问题的根本原因) :
const { ref, computed, onMounted } = Vue; let options = { setup: function () { let dashboards = ref({}); const reportStatus = computed(function() { return (dashboards.value.status === 'Production')? 'status--production': 'status--qa' }); onMounted(function() { dashboards.value = { report: "Ad Hoc", description: "", status: "Production" } }); return { dashboards, reportStatus }; } }; let appVue = Vue.createApp(options).mount('#app');
.status--production { background-color: blue; color: white; border: 1px solid black; }.status--qa { background-color: green; color: white; border: 1px solid black; }
<script src="https://unpkg.com/vue@3.0.0-beta.14/dist/vue.global.js"></script> <div id="app"> <span:class="reportStatus"> {{ dashboards.status }} </span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.