Created
September 25, 2017 03:11
-
-
Save curder/adfd568c01bc02ac724ef5159e6f4334 to your computer and use it in GitHub Desktop.
使用Vue2.4.2开发Todo应用Demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Vue js 2.0</title> | |
<link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"> | |
<style type="text/css"> | |
.text-decoration{ | |
text-decoration:line-through; | |
} | |
</style> | |
</head> | |
<body> | |
<nav class="navbar navbar-light bg-light"> | |
<span class="h4" class="navbar-brand mb-0">Learn Vue js 2.0</span> | |
</nav> | |
<div id="app" class="container"> | |
<div class="card"> | |
<div class="card-header"><h4>My Todo List [ {{ todoCount }} ]</h4></div> | |
<div class="card-body"> | |
<ul class="list-group"> | |
<li class="list-group-item" | |
v-bind:class="{'text-muted text-decoration' : todo.computed}" | |
v-for="(todo, index) in todos"> | |
{{ todo.title }} | |
<button v-on:click="deleteTodo(index)" | |
class="btn btn-danger btn-sm float-right">Delete</button> | |
<button v-on:click="toggleTodo(todo)" | |
v-bind:class="[ todo.computed ? 'btn-warning' : 'btn-primary' ]" | |
class="btn btn-sm float-right mr-2">{{ todo.computed ? 'Undo' : 'Computed' }}</button> | |
</li> | |
</ul> | |
</div> | |
<div class="card-footer"> | |
<form v-on:submit.prevent="addTodo(newTodo)"> | |
<div class="col-lg-6"> | |
<div class="input-group"> | |
<input type="text" v-model="newTodo.title" class="form-control" placeholder="Add Todo"> | |
<span class="input-group-btn"> | |
<button class="btn btn-secondary">Add Todo</button> | |
</span> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</body> | |
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script> | |
<script type="text/javascript"> | |
new Vue({ | |
el: '#app', | |
data: { | |
todos: [{id: 1, title: 'Learn VueJs2.0.', computed: false}, {id: 2, title: 'Study English.', computed: true},], | |
newTodo: {id: null, title: '',computed: false} | |
}, | |
computed: { | |
todoCount() { | |
return this.todos.length; | |
} | |
}, | |
methods: { | |
addTodo(newTodo) { | |
if(!newTodo.title) { | |
alert('请输入内容'); | |
return ; | |
} | |
this.todos.push(newTodo) | |
this.newTodo = {id: null, title: '',computed: false} | |
}, | |
deleteTodo(index) { | |
this.todos.splice(index, 1); | |
}, | |
toggleTodo(todo) { | |
todo.computed = ! todo.computed | |
} | |
} | |
}) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment