Skip to content

Instantly share code, notes, and snippets.

@jfbrennan
Last active March 29, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfbrennan/1765ae2236a50e08602cc99f894b9165 to your computer and use it in GitHub Desktop.
Save jfbrennan/1765ae2236a50e08602cc99f894b9165 to your computer and use it in GitHub Desktop.
Vue Single File Component with all the basics
<template>
<p>Todo count: {{todos.length}}</p>
<input ref="newTodo" type="text">
<button v-on:click="add">Add</button>
<ul v-if="todos.length">
<li v-for="(todo, i) of todos" :key="i">{{todo}} <button v-on:click="remove(i)">Remove</button></li>
</ul>
</template>
<script>
export default {
name: 'my-todos',
// Lifecycle methods: https://vuejs.org/v2/api/#Options-Lifecycle-Hooks
data () {
return {
todos: []
}
},
methods: {
add(e) {
const todo = this.$refs.newTodo.value;
this.todos.push(todo);
},
remove(i) {
this.todos.splice(i, 1);
}
}
}
</script>
<style scoped>
ul { list-style: none }
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment