Vue组建化
Last updated
Last updated
<app-section
:todos="filterTodos"></app-section>
//子组件中声明对应的属性
exports.appSection={
props:['todos','toggleAll']
}//header 模板内容
const header_template=`<header >
<h1 >todos</h1>
<input class="new-todo" placeholder="What needs to be done?" v-model="newtodo" @keyup.enter="@keyup.enter="heanderNewtodo"" @blur="@keyup.enter="heanderNewtodo"" >
</header>`
//子组件中定义好新的数据变量,以及向上进行传递数据
appheader={
props:['todos'],
data(){
return({
newtodo:''
})
},
template:header_template,
methods:{
heanderNewtodo(){
this.$emit('new-todos',this.newtodo) //传递数据给父组件
this.newtodo='' //将子组件输入框中内容清空
}
}
}//模板内容
<app-header @new-todos="handerNewTodos($event)"
></app-header>
//事件的处理
handerNewTodos(newtodo){
var value=newtodo && newtodo.trim()
if(!value){
return
}
this.todos.push({
id: this.todos.length ? this.todos[this.todos.length-1].id+1 : 1,
completed: false,
title:value
})
}