반응형
Vue JS - 하나의 구성 요소에 조건부로 정보를 표시
나는 일련의 오브젝트에 포함된 데이터를 반응적으로 표시하는 구성요소를 가지고 있다.현재 부품에 두 개 모두 표시되지만, 조건에 따라 표시했으면 한다.프리미엄이라는 속성의 상태를 기준으로 하나 또는 다른 개체를 표시하는 방법활성화?
<template>
<div>
<div class="background-div">
<div class="page-info-inner">
<PremiumContent v-for="item in premiumContent"
:key="item.infoText"
:info-text="item.infoText"
:button-text="item.buttonText"
:button-callback="null"
:subscription-text="item.subscriptionText" />
</div>
</div>
</div>
</template>
<script>
import PremiumContent from '../../../components/partials/settings/Premium';
export default {
name: 'MyComponent',
components: {PremiumContent},
data: () => ({
premiumActivated: false,
premiumContent: [
{
infoText: "this is the content that should appear if premiumActivated is false",
buttonText: "button text",
},
{
infoText: "this is the content that should appear if premiumActivated is true",
buttonText: "button text",
subscriptionText: 'extra text',
},
]
}),
Vue에서 계산된 속성을 사용하여 어레이에서 표시할 개체를 결정할 수 있다.
computed: {
displayContent () {
return this.premiumActivated ? premiumContent[1] : premiumContent[0]
}
}
PremiumContent는 다음과 같이 표시됨:
<PremiumContent
:key="displayContent.infoText"
:info-text="displayContent.infoText"
:button-text="displayContemt.buttonText"
:button-callback="null"
:subscription-text="displayContent.subscriptionText" />
편집: 또 다른 해결책은computed
또는mounted
낚싯바늘을 달다
computed () {
this.item = this.premiumActivated ? premiumContent[1] : premiumContent[0]
}
반응형
'Programing' 카테고리의 다른 글
GDB가 어떤 주소에 문제가 발생했는지 알려주려면 어떻게 해야 할까? (0) | 2022.04.29 |
---|---|
TypeError: 정의되지 않은 Vuejs의 속성 'posts'를 설정할 수 없음 (0) | 2022.04.28 |
어떻게 하면 한 끈이 다른 끈을 교체하는 것으로 끝나지 않는 방식으로 두 줄을 교체할 수 있을까? (0) | 2022.04.28 |
C에 대한 설계 원리, 모범 사례 및 설계 패턴(또는 일반적으로 절차 프로그래밍)? (0) | 2022.04.28 |
vue 구성 요소에서 href로 조건을 추가하려면 어떻게 해야 하는가? (0) | 2022.04.28 |