<template>
<button class="btn btn1 active-btn" @click ="setActiveBtn">Limit</button>
<button class="btn btn2" @click ="setActiveBtn">Market</button>
</template>
export default {
components: {
name: 'LeftBlock'
},
methods: {
setActiveBtn: function () {
const btn1 = document.querySelector('.btn1')
const btn2 = document.querySelector('.btn2')
if (btn1.classList.contains('active-btn')) {
btn1.classList.remove("active-btn")
btn2.classList.add("active-btn")
} else {
btn1.classList.add("active-btn")
btn2.classList.remove("active-btn")
}
}
}
}
<template>
<button
class="btn btn1"
:class="{'active-btn': currentActive === 'limit'}"
@click="currentActive='limit'">
Limit
</button>
<button
class="btn btn2"
:class="{'active-btn': currentActive === 'market'}"
@click="currentActive='market'">
Market
</button>
</template>
<script>
export default {
data() {
return {
currentActive: 'limit'
}
}
}
</script>
data: () => ({
buttons: [
{ className: 'btn1', text: 'Limit' },
{ className: 'btn2', text: 'Market' },
],
active: 0,
}),
<button
v-for="(n, i) in buttons"
v-text="n.text"
:class="[ 'btn', n.className, { 'active-btn': i === active } ]"
@click="active = i"
></button>
btn*
действительно идут по порядку)data: () => ({
buttons: [ 'Limit', 'Market' ],
active: 'Limit',
}),
<button
v-for="(n, i) in buttons"
:class="`btn btn${i + 1} ${n === active ? 'active-btn' : ''}`"
@click="active = n"
>{{ n }}</button>