包含内容
- 数据的双向绑定
- 父组件向子组件传值
- 子组件向父组件传值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="inputValue" />
<button v-on:click="handleBtnClick">提交</button>
<ul>
<!-- 【父组件向子组件向传值】通过v-bind -->
<todo-item
v-bind:content="item"
v-bind:index="index"
v-for="(item, index) in list"
@delete="handleItemDelete"><!-- 【子组件向父组件传值2】父组件中创建子组件,同时监听删除时间 -->
</todo-item>
</ul>
</div>
<script>
//全局组件
// Vue.component("TodoItem", {
// props:['content'],
// template: "<li>{{content}}</li>"
// })
//局部组件
var TodoItem = {
props: ['content', 'index'],
template: "<li @click='handleItemClick'>{{content}}</li>",
methods: {
handleItemClick: function () {
this.$emit("delete", this.index) //【子组件向父组件传值1】,通过【$emit】向外触发事件
}
}
}
var app = new Vue({
el: '#app',
components: {
TodoItem: TodoItem
},
data: {
list: [],
inputValue: ""
},
methods: {
handleBtnClick: function () {
this.list.push(this.inputValue)
this.inputValue = ""
},
handleItemDelete: function (index) {
this.list.splice(index, 1);
}
}
})
</script>
</body>
</html>
需要视频资料的老哥请留言