I have a component that renders text based on the users membership status and I want to change the interpolated text based on that prop value. Is there a more efficient way of showing different text based on the prop besides a bunch divs/p tags withv-if or v-show?

It's just a ton of text to constantly have a bunch of stacked divs.

Any advice would be appreciated!

Cheers!

<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>

推荐答案

Maybe something like following snippet (map your plans and use computed property ):

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相关问答推荐

我可以在useState中调用函数并使用其数据吗

使用NgDeliverentOutlet和动态内容投影的Angular 渲染组件

为什么有些库公开了执行相同任务的方法,但每个方法都处于同步/同步上下文中?

为什么在集内和集外会产生不同的promise 状态(使用Promise.race)?

为什么使用MAX_SAFE_INTEGER生成随机整数时数字分布不准确?

如何编辑代码FlipDown.js倒计时?

将数据从strapi提取到next.js,但响应延迟API URL

*ngFor和@代表输入decorator 和选角闭合

具有相同参数的JS类

如何在表格上拥有水平滚动条,在正文页面上拥有垂直滚动条,同时还对html表格的标题使用位置粘性?

在Angular中将样式应用于innerHTML

MongoDB中的引用

如何将Map字符串,布尔值转换为{key:string;value:bo布尔值;}[]?<>

在react JS中映射数组对象的嵌套数据

JSDoc创建并从另一个文件导入类型

VUE 3捕获错误并呈现另一个组件

如何发送从REST Api收到的PNG数据响应

未加载css colored颜色 ,无法将div设置为可见和不可见

如何在Highlihte.js代码区旁边添加行号?

JQuery使用选项填充HTMLSELECT并设置默认结果,默认结果显示为空