亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

在Vue中如何根據(jù)數(shù)據(jù)增加圖標(biāo)數(shù)量
P粉832490510
P粉832490510 2024-01-10 17:03:43
0
2
530

我發(fā)送了一個(gè)API請(qǐng)求并獲得了不同數(shù)量的參與者(從1到5)。根據(jù)這個(gè)數(shù)量,在模態(tài)框中應(yīng)該顯示相應(yīng)數(shù)量的圖標(biāo)。如果有3個(gè)參與者,則模態(tài)框中應(yīng)該有3個(gè)用戶圖標(biāo)。 我知道如何在React中實(shí)現(xiàn),但我開(kāi)始學(xué)習(xí)Vue3,不知道該怎么做。

<template>
  <p>
        參與者:
            <span> 
                <UserIcon />
           </span>
  </p>
</template>


 <script lang="ts">
    import { defineComponent } from '@vue/runtime-core';
    import {HeartIcon, UserIcon, XIcon } from '@heroicons/vue/solid'
    
    export default defineComponent({
        name: 'NewActivityModal',
        components: {HeartIcon, UserIcon, XIcon},
        props: {
            randomActivity: {
                type: Object,
                required: true,
            }
        },            
    })
    
      </script>

在React中,我會(huì)這樣寫(xiě)。但在Vue中該如何重寫(xiě)?明確一下,應(yīng)該放在哪里?

const participants = [
    <span>
      {userIcon}
    </span>,
  ];
  randomActivity.map((item) => {
    for (let i = 1; i < item.participants; i++) {
      participants.push(
        <span className="inline-block" key={`personIcon${i}`}>
          {userIcon}
        </span>
      );
    }
    return participants;
  });
P粉832490510
P粉832490510

全部回復(fù)(2)
P粉647449444

感謝 @tho-masn 的幫助,我能夠理解并重新編寫(xiě)計(jì)算屬性

numberOfParticipants () {           
        let participants = 1
        for(let i=1; i < this.randomActivity.participants; i++) {
          participants += 1;
        }       
        return participants
      }
P粉434996845

首先,您需要?jiǎng)?chuàng)建一個(gè)計(jì)算屬性,該屬性返回參與者的數(shù)量(循環(huán)的計(jì)數(shù)器 x)。

然后,您使用該計(jì)算屬性運(yùn)行循環(huán) x 次。

這是您想要實(shí)現(xiàn)的嗎?

<template>
  <p>
    參與者:
    <span
      v-for="counter in numberOfParticipants"
      :key="counter"
    > 
      <UserIcon />
    </span>
  </p>
</template>


<script lang="ts">
  import { defineComponent } from '@vue/runtime-core';
  import {HeartIcon, UserIcon, XIcon } from '@heroicons/vue/solid'
  
  export default defineComponent({
    name: 'NewActivityModal',
    components: {HeartIcon, UserIcon, XIcon},
    props: {
      randomActivity: {
          type: Object,
          required: true,
        }
      }
    }, 
    computed: {
      numberOfParticipants () {
        return randomActivity
          .map(item => {
            return item.participants
          })
          .flat()
          .length
      }    
    } 
  })
</script>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板