我有一个组件,根据用户成员身份状态呈现文本,我想根据该属性值更改插值文本.除了一堆v-ifv-show的divs/p标签外,还有没有更有效的方式根据props 显示不同的文本?

总是有一堆叠在一起的div就是一堆文本.

任何建议都将不胜感激!

干杯

<script lang="ts" setup>
import { PropType } from 'vue'

const props = defineProps({
  kind: {
    type: String as PropType<'subscribed' | 'unsubscribed'>,
    default: 'subscribed',
  },
  planId: {
    type: String as PropType<'standard' | 'silver' | 'gold' | 'platinum' | 'diamond' | 'no plan'>,
    default: 'standard',
  },
})
</script>

<template>
  <div class="c-promotion-plan-card" data-cy="component-promotion-plan-card">
    <div class="flex items-baseline mb-sm">
      <div v-if="planId === 'standard'" class="text-h6 text-dark">Standard Gang</div>
      <div v-if="planId === 'silver'" class="text-h6 text-dark">Silver Foxes</div>
      <div v-if="planId === 'gold'" class="text-h6 text-dark">Golden Girls</div>
      <div v-if="planId === 'platinum'" class="text-h6 text-dark">Platinum Boys</div>
      <div v-if="planId === 'diamond'" class="text-h6 text-dark">Diamond Dudes</div>
      <div v-if="planId === 'no plan'" class="text-h6 text-dark">
        No Plan Posse
      </div>
    </div>
  </div>
</template>

推荐答案

可能类似于以下片段(映射您的计划并使用计算(computed)属性):

const { ref, computed } = Vue
const app = Vue.createApp({
  props: {
    kind: {
      type: String,
      default: 'subscribed',
    },
    planId: {
      type: String,
      default: 'standard',
    },
  },
  setup(props) {
    const plans = ref([{plan:'standard', name: 'Standard Gang'}, {plan:'silver', name: 'Silver Foxes'}, {plan:'gold', name: 'Golden Girls'}, {plan:'platinum', name: 'Platinum Boys'}, {plan:'diamond', name: 'Diamond Dudes'}, {plan: 'no plan', name: 'No Plan Posse'}])
    const handlePlan = computed(() => plans.value.find(p => p.plan === props.planId))
    return { plans, handlePlan }
  },
})
app.mount('#demo')
.style-class {
  color: red;
  font-size: 2em;
}
.other-style {
  color: blue;
}
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <div class="c-promotion-plan-card" data-cy="component-promotion-plan-card">
    <div class="flex items-baseline mb-sm">
      <div class="text-h6 text-dark" :class="handlePlan.plan === 'standard' ? 'style-class' : 'other-style'">{{ handlePlan.name }}</div>
    </div>
  </div>
</div>

Javascript相关问答推荐

如何才能拥有在jQuery终端中执行命令的链接?

我可以使用CSS有效地实现最大宽度=100%和最大高度=100%,而无需父母有明确的/固定的宽度和高度,替代方法吗?

jQuery s data()[storage object]在vanilla JavaScript中?'

使用GraphQL查询Uniswap的ETH价格

JS—删除对象数组中对象的子对象

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

使用原型判断对象是否为类的实例

如何在Angular17 APP中全局设置AXIOS

如何强制Sphinx中的自定义js/css文件始终加载更改而不缓存?

rxjs插入延迟数据

为什么JPG图像不能在VITE中导入以进行react ?

如何确保预订系统跨不同时区的日期时间处理一致?

如何在HTMX提示符中设置默认值?

在将元素追加到DOM之前,createElement()是否会触发回流?混淆abt DocumentFragment行为

如何使用Cypress在IFRAME中打字

MAT-TREE更多文本边框对齐问题

ReactJS Sweep Line:优化SciChartJS性能,重用wasmContext进行多图表渲染

如何使用Angular JS双击按钮

在特定区域的图像上 Select x和y,并在其上创建边框

如何循环通过一个参数的键,该参数可以是TypeScrip中三个不同接口之一?