0

I have an array of color in my vuex state and in my component I want to specify each color for each element that comes from v-for.

// state
state: {
    APIData: {
      userInfo: {},
      allClasses: [
        {
          subject: 'subject1',
          ...
        },
        {
          subject: 'subject2',
          ...
        },
        {
          subject: 'subject3',
          ...
        }
      
      ],
      classBackGroud: ['red', 'blue', 'green',..],
    },
  },

here is what i have tried

// component
<v-card
   class="col-12"
   v-for="(course, index) in APIData.allClasses"
   :key="course.subject"
   v-bind:style="{color: APIData.classBackGround[index]}"
>
   {{ course.subject }}
</v-card>

I want something like this, subject1 has color red, subject2 has color blue, subject3 has color green, ..etc.

With what i have tried i got this error

[Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined"

How can achieve it?

1 Answer 1

2

Your state object has classBackGroud but you are trying to render classBackGround. Seems like a spelling mistake. Otherwise, the code looks good to me. Try rectifying the property name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.