<div class="container" id="app">
<div class="card">
<h3 class="card-header">Todos 数量:{ todoCount }</h3>
<div class="card-block">
<todo-items :todos="todos"></todo-items>
</div>
<div class="card-block">
<todo-form :todos="todos"></todo-form>
</div>
</div>
</div>
<script type="text/x-template" id="todo-items-template">
<ul class="list-group">
<li class="list-group-item" v-bind:class="{ 'completed': todo.completed }" v-for="(todo,index) in todos">
{ todo.title }
<button type="button" class="btn btn-sm" v-bind:class="[todo.completed ? 'btn-success': 'btn-danger']" v-on:click="toggleCompletion(todo)">{ todo.completed ? 'completed': 'undo' }</button>
<button type="button" class="btn btn-warning btn-sm" v-on:click="deleteTodo(index)">删除</button>
</li>
</ul>
</script>
<script type="text/x-template" id="todo-add-form-template">
<form v-on:submit.prevent="addTodo(newTodo)">
<div class="form-group row">
<label for="input1" class="col-sm-2 col-form-label">输入内容</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="input1" v-model="newTodo.title">
</div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-10">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</div>
</form>
</script>
<script>
Vue.component('todo-items',{
template:"#todo-items-template",
props:['todos'],
methods:{
deleteTodo(index){
this.todos.splice(index,1);
},
toggleCompletion(todo){
todo.completed = !todo.completed
}
}
});
Vue.component('todo-form',{
template:"#todo-add-form-template",
props:['todos'],
data(){
return{
newTodo: {id:null,title:'',completed:false}
}
},
methods:{
addTodo(newTodo){
this.todos.push(newTodo);
this.newTodo={id:null,title:''};
}
}
});
new Vue({
el: "#app",
data:{
todos:[
{id:1,title:'learn vue.js 2.0',completed:false}
]
},
computed:{
todoCount(){
return this.todos.length;
}
}
});
</script>
问题:
props:['todos']中的todos
对应的下面两个标签中的:todos="todos"
,是对应等号前面的todos还是后面的?
<todo-items :todos="todos"></todo-items>
<todo-form :todos="todos"></todo-form>