[Vuejs]-How to set different style to only one element generated dynamically in Vue

0👍

How about storing active_index in your parent component and pass it to your New_page

eg:

<template>
  <div class="navbar">
    <div class="list_pages">
      <New_page v-for="index in page_counter" :key="index" :index="index" :is_active="is_active(index)" @clicked="foo"/>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      active_index: 0
    }
  },
  methods: {
    is_active(index) {
      return index === this.active_index
    },
    foo(index) {
      this.active_index = index
    }
  }
}
</script>

In the NewPage file you can emit event to change active index in the parent component eg:

<template>
  <div class="pad">
    <div class="card" @click="change_style()" :style="{
        backgroundColor: is_active ? 'red' : '#5fc9f3'
      }" v-if="!new_p">
      <p>{{index}}</p>
    </div>
</template>

<script>
export default {
  props: ['index', 'is_active'],
  emits: ['clicked'],
  methods: {
    change_style() {
      this.$emit('clicked', this.index)
    }
  }
}
</script>

Leave a comment