If you're faced with this situation, there are a couple of ways you can handle it. Let me walk you through them here. One option is to include a basic check using v-if
to determine if the item exists in the modules
array. For example:
<div class="abc box" v-if="modules.indexOf('abc') >= 0">abc</div>
<div class="def box" v-if="modules.indexOf('def') >= 0">def</div>
...
This method may not be the most elegant solution in terms of maintenance or performance. Another approach would involve using the modules
array as a reference for generating your collection of div
elements, utilizing v-for
. Here's an example:
<div class="box" v-for="module in modules" :class="module">{{ module }}</div>
Take note of the following key points:
- The
v-for
directive relies on the modules
array to dictate the number of divs to be rendered.
- The value within the array is used both as content for the div (
{{ module }}
) and as one of the classes (:class="module"
).
- The class
box
is consistently applied using the standard class
attribute. The combination of this and the :class
binding results in something like abc box
as the list of classes.
Below is a functional demonstration showcasing the v-for
approach.
new Vue({
el: '#app',
data: {
modules: ['abc', 'def']
}
});
.box {
height: 75px;
width: 75px;
background: #444;
padding: 10px;
margin: 5px;
color: #fff;
}
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0969585a0d2ced5ced6">[email protected]</a>/dist/vue.js"></script>
<div id="app">
<div class="box" v-for="module in modules" :class="module">{{ module }}</div>
</div>