반응형
v-for 루프를 사용하여 다른 색상 배경 바인딩
나는 부에티피(Vuetify)를 사용하고 배경으로 적용하고자 하는 다양한 헥스코드가 포함된 자바스크립트 오브젝트를 통해 반복하려고 한다.
최종 결과는 다음과 같이 보일 것이다.
나는 각각의 Hexcode를 각각의 다른 아이템 컬러의 배경에 묶으려고 한다.
아래는 내가 어떻게 일을 하려고 하는지에 대한 것이다.
<!-- Color Cards -->
<v-container grid-list-md text-xs-center>
<v-layout row wrap>
<v-flex class="item" xs12 sm4 v-for="color in colors" :key="color.id">
<v-card ripple hover class="">
<div class="item-color"
v-for="(hex, index) in colors.hex"
:key="index"
:style="{ 'background-color': hex[index]}">
</div>
<v-card-text class="px-0">{{ color.title }}</v-card-text>
</v-card>
</v-flex>
</v-layout>
</v-container>
다음은 데이터 개체:
export default {
data () {
return {
colors: [
{
id: 'ssmf',
hex: ['#297afb','#2898fb','#01d8fd'],
title: 'Sleek, Sophisticated, Mature & Formal'
},
{
id: 'hlfss',
hex: ['#297afb','#2898fb','#01d8fd'],
title: 'Honest, Loyal, Friendly, Stable, & Strong'
}
]
}
}
}
두 가지 실수가 있다.
- 인
<v-flex>
당신은 반복해서v-for="color in colors"
그렇게color
반복 중인 어레이 항목의 별칭colors
배열. 그러나 인v-card>
원소의div
반복하고 있는 태그colors.hex
그래서 그것은 그래야만 한다.v-for="(hex, index) in color.hex"
아닌colors.hex
hex
다음에서 반복되는 항목color.hex
끈이잖아그래서 직접 사용할 수 있고, 필요 없다.hex[index]
<v-container grid-list-md text-xs-center> <v-layout row wrap> <v-flex class="item" xs12 sm4 v-for="color in colors" :key="color.id"> <v-card ripple hover class=""> <div class="item-color" v-for="(hex, index) in color.hex" :key="index" :style="{ 'background-color': hex}"> </div> <v-card-text class="px-0">{{ color.title }}</v-card-text> </v-card> </v-flex> </v-layout> </v-container>
이미 반복되고 있으므로 v-for 루프를 약간 변경하십시오.color.hex
참조할 것은 육각형뿐이다.
<div v-for="color in colors">
<div class="item-color"
v-for="(hex, index) in color.hex"
:key="index"
:style="{ 'background-color': hex}">
</div>
</div>
여기 일하는 fiddle https://jsfiddle.net/skribe/00cf8z7x/4/
구문을 잘 읽으려면 객체의 16진수를 16진수로 변경하십시오.
colors: [
{
id: 'ssmf',
hexs: ['#297afb','#2898fb','#01d8fd'],
title: 'Sleek, Sophisticated, Mature & Formal'
},
....
그러면 당신은 당신의 v-for를 다음과 같이 쓸 수 있다.v-for="hex in color.hexs"
참조URL: https://stackoverflow.com/questions/49463729/binding-different-color-backgrounds-using-v-for-loop
반응형
'programing' 카테고리의 다른 글
Nuxt / Vue - 돌연변이 처리기 외부에서 vuex 저장소 상태 변경 안 함 (0) | 2022.05.21 |
---|---|
if-return-return 또는 if-else-return을 사용하는 것이 더 효율적인가? (0) | 2022.05.21 |
VUE CLI-3 프로젝트가 IE-11에서 작동하지 않음 (0) | 2022.05.21 |
Nuxt는 페이지를 렌더링하고 반환하기 전에 Vuex 상태를 업데이트해야 하지 않을까? (0) | 2022.05.21 |
돔 내에서 Vue 구성 요소를 이동하시겠습니까? (0) | 2022.05.21 |