<div id="wrap">
<header id="navigation-top">
<nav class="main-navigation" v-on:click.prevent>
<!-- this is the button that toggles the class "active" -->
<button v-on:click="makeActive('container')" class="navigation-hamburger" :class="isActive ? 'active' : ''">
+
</button>
<button v-on:click="makeActive('container-fluid')" class="navigation-hamburger" :class="isActive ? 'active' : ''">
-
</button>
</nav>
</header>
<!-- this is the div that needs to get the class "active" when the button is clicked -->
<div v-bind:class="active">
<ul>
<li><a href="#" class="links">Home</a></li>
<li><a href="#" class="links">About</a></li>
<li><a href="#" class="links">Contact</a></li>
</ul>
</div>
</div>
new Vue({
el: '#wrap',
data: {
active: 'container',
isActive: true
},
// Functions we will be using.
methods: {
makeActive: function(item){
// When a model is changed, the view will be automatically updated.
this.active = item;
}
}
});
data: () => ({
items: [
{ buttonText: '+', className: 'container' },
{ buttonText: '-', className: 'container-fluid' },
],
...
}),
<button
v-for="n in items"
v-text="n.buttonText"
:class="{ active : active === n.className }"
@click="active = n.className"
></button>
item
и active
- плохие названия для переменных, хранящих класс, makeActive
- плохое название метода, его меняющего. Называем все по-людски.data: {
activeClass: 'container'
...
},
methods: {
setActiveClass: function(className) { ... }
}
data: {
activeClass: 'container',
buttons: [
['+', 'container'],
['-', 'container-fluid']
]
}
<button v-for="[label, className] in buttons">...</button>
data
нет необходимости. <button
v-for="[label, className] in buttons"
v-on:click="setActiveClass(className)"
v-bind:class="{ active: className === activeClass }">
{{label}}
</button>