简体   繁体   English

我如何使用计算属性根据单词使用 vue 组合 api 来更改文本药丸的颜色?

[英]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.

相关问题 Vue组合API计算属性测试 - Vue composition API computed property test Vue 3 组合 api - 计算属性返回未定义 - Vue 3 composition api - computed property is returning undefined 如何使用组合 API 创建和使用计算属性的设置器 - How to create and use computed property's setter with the composition API 如何在新组合 API 中键入计算属性? - How to type a computed property in the new composition API? vue 组合 API - ref 变量内的计算属性 - vue composition API - computed property inside ref variable Vue 3:计算属性不跟踪其在组合 API 中的依赖关系 - Vue 3: computed property doesn't track its dependency in composition API 使用 Composition API 和 Script Setup 语法时,如何让 refs 和计算变量显示在 Vue Devtools 中? - How do I get refs and computed vars to show up in Vue Devtools when using the Composition API and the Script Setup syntax? 我如何使用vue-router在子Vue组件中使用计算属性 - How do I use a computed property in a child Vue component using vue-router 如何更改计算属性的值? - How can I change the value of a computed property? 计算属性的返回值在模板中使用时不会改变(没有组合 API 的 Vue3) - Returned Value From Computed Property Doesn't Change When It's Used In Template (Vue3 without Composition API)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM