Vue.js 2 - Only one checkbox

by Hugo Carneiro

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<div id="app">
  <input type="checkbox" name="vehicle1" value="Bike" v-model="items"> I have a bike<br>
  <input type="checkbox" name="vehicle2" value="Car" v-model="items"> I have a car<br>
  <input type="checkbox" name="vehicle3" value="Boat" v-model="items"> I have a boat<br>
</div>

Babel + JSX

new Vue({
  el: '#app',
  data: {
    items: ['Car']
  },
  watch: {
  	items(newVal, oldVal) {
    	// Do nothing if the array is empty or already only has one value
    	if (newVal.length <= 1) {
      	return
      }
      
      // Otherwise, remove the first item in the array
      newVal.shift()
    }
  }
})