首页  >  问答  >  正文

在 Vue 中基于 props 显示有条件渲染的文本的替代方法?

我有一个根据用户成员资格状态呈现文本的组件,我想根据该属性值更改插值文本。除了使用 v-ifv-show 的一堆 div/p 标签之外,是否有更有效的方法根据 prop 显示不同的文本?

不断地有一堆堆叠的 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>

P粉021854777P粉021854777205 天前412

全部回复(2)我来回复

  • P粉447002127

    P粉4470021272024-03-28 16:45:31

    是的,有更好的方法。 您可以定义一个计算属性,例如 Vue2 语法

    computed: {
     getLabel() {
      // Assuming that 'planId' is the dependency prop
      if(this.planId === 'standard') return 'Standard Gang';
      else if(this.planId === 'silver') return 'Silver Foxes';
      ....
      return 'No Plan Posse' // For 'no plan' condition
     }

    Vue3 语法

    setup(props) {
     // 1.getLabel depends on firstName,lastName.
     const getLabel = computed(() => {
      // Assuming that 'planId' is the dependency prop
      if(props.planId === 'standard') return 'Standard Gang';
      else if(props.planId === 'silver') return 'Silver Foxes';
      ....
      return 'No Plan Posse' // For 'no plan' condition
     });
     return {
      getLabel,
     };
    },

    然后在模板内调用插值计算,例如

    {{getLabel}}

    回复
    0
  • P粉245276769

    P粉2452767692024-03-28 13:58:39

    也许类似于以下代码片段(映射您的计划并使用计算属性):

    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;
    }
    sssccc
    
    {{ handlePlan.name }}

    回复
    0
  • 取消回复