P
P
picka2021-03-11 18:46:44
Vue.js
picka, 2021-03-11 18:46:44

How to open dynamically created elements?

There is a code

<ul>
                    <li v-for="(Item, Index) in Navigation" :key="Index">
                        <div>
                            <Nuxt-link :to="Item.Link">{{Item.Name}}</Nuxt-link>
                            <span @click="SubNavActive = !SubNavActive"></span>
                        </div>
                        <ul :class="{active: SubNavActive}">
                            <li v-for="(Item, Index) in Item.SubNav" :key="Index">
                                <Nuxt-link :to="Item.Link">{{Item.Name}}</Nuxt-link>
                            </li>
                        </ul>
                    </li>
                </ul>


and JavaScript
<script>
export default {
    data () {
        return {
            SubNavActive: false,
        }
    }
}
</script>


How to open subnav without being tied to a specific variable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Velichko, 2021-04-01
@Zoxon

index for loops is better not to use, add a field with a unique id to the object and use it instead of index. And it's also worth everything that is possible to take out of the template
Save the id of the item this.activeItem = index
The code for the active class will look like this :class="{ active: activeItem === index }"
PS.
Can CSS hover or focus be used?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question