Skip to content

Instantly share code, notes, and snippets.

@rye761
Created June 23, 2017 17:43
Show Gist options
  • Save rye761/ae7743c7e9c153df5848bc8285849083 to your computer and use it in GitHub Desktop.
Save rye761/ae7743c7e9c153df5848bc8285849083 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Todo App</title>
<meta charset="utf-8">
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<h1>{{ header }}</h1>
<todo-form @add-todo="addTodo"></todo-form>
<todo-list :todos="todos"></todo-list>
</div>
<script>
Vue.component('todo-form', {
template: '<form @submit.prevent="addTodo"><input type="text" v-model="newTodo"><input type="submit" value="Add"></form>',
data: function () {
return {
newTodo: ''
};
},
methods: {
addTodo: function() {
this.$emit('add-todo', this.newTodo);
this.newTodo = '';
}
}
});
Vue.component('todo-list', {
template: '<ul><li v-for="todo in todos">{{ todo }}</li></ul>',
props: ['todos']
});
new Vue({
el: '#app',
data: {
'todos': [],
'header': 'Todo App'
},
methods: {
addTodo: function(todo) {
this.todos.push(todo);
}
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment