I want to create a webpage containing several button groups. The existing code is
$(document).ready(function() {
$(".nav-link").click(function() {
console.log(this);
});
});
Vue.component('nav-bar', {
props: ['navs'],
template: `<ul class="nav">
<li v-for="nav in [{id: 0, text: 'Vege'}, {id:1, text: 'Ch'}]"
v-bind:item="nav"
v-bind:key="nav.id">
<a class="nav-link" v-on:click="setactive($event)">{{ nav.text }}</a></li>
</ul>`,
methods: {
setactive: function(event) {
$(event.target).closest('ul').find('a.active').removeClass('active');
$(event.target).addClass('active');
},
created: function() {
$(this).find('a').first().addClass('active');
}
}
});
var app = new Vue({
el: '#app',
data: {
navs: [
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Milk' }
]
}
});
.nav {
display: flex;
flex-wrap: wrap;
list-style: none;
}
.nav-link.active {
color: #fff;
background-color: #007bff;
}
.nav-link {
color: #4183c4;
text-decoration: none;
background-color: transparent;
border-radius: .25rem;
padding: .5rem 1rem;
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="app">
<nav-bar></nav-bar>
</div>
My problems are:
- if changing
<li v-for="nav in [{id: 0, text: 'Vege'}, {id:1, text: 'Ch'}]"to<li v-for="nav in navs", nothing is displayed at all. Whats wrong with that? - why isn't the first item's class set to
activeupon creation? - how can the nav items be given directly to
<nav-bar>?
I do not want to use bootstrap button-groups.