一、实现效果

二、实现代码

HelloWorld.vue

<template>
<div class="hello">
<child-page v-for="(item,index) in items"
:key="index"
:index="index"
:items="items"
@deleteIndex="del"
@uploadData="getData">
</child-page>
<button @click="add">Add</button>
</div>
</template> <script>
import ChildPage from './ChildPage'
export default {
data () {
return {
items: [{}],
dataRec: []
}
},
components: {
ChildPage
},
methods: {
// add student
add: function () {
this.items.push({name: '', age: ''})
},
// delete student
del: function (index) {
// not allow to delete the first
if (index !== 0) {
this.items.splice(index, 1)
console.log('deleted:', JSON.stringify(this.items))
}
},
// get the data from child
getData: function (val) {
let index = val.index
this.items[index] = val.data
console.log('I got the data:', JSON.stringify(this.items))
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

ChildPage.vue

<template>
<div class="hello">
<h1>Component:{{index}}</h1>
<p>Name:<input type="text" v-model="student.name" placeholder="Please enter name"></p>
<p>Age:<input type="text" v-model="student.age" placeholder="Please enter age"><button @click="deleteStudent">Delete</button></p>
</div>
</template> <script>
export default {
props: {
index: {
type: Number,
required: true
},
items: {
type: Array,
default: Array
}
},
data () {
return {
student: {
name: '',
age: ''
}
}
},
watch: {
student: {
handler (newV, oldV) {
if (newV.name.length === 0) {
return false
}
if (newV.age.length === 0) {
return false
} this.$emit('uploadData', {index: this.index, data: newV})
},
deep: true
},
items: {
handler (newV, oldV) {
if (newV.length !== 0) {
this.student = {...newV[this.index]}
}
},
deep: true
}
},
methods: {
deleteStudent: function () {
this.$emit('deleteIndex', this.index)
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

最新文章

  1. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 角色权限的配置页面改进优化
  2. C#进阶系列——DDD领域驱动设计初探(一):聚合
  3. Quartz与Spring整合进行热部署的实现(一)
  4. 活跃天数计算用户等级模仿QQ的升级方式
  5. Cocos2d-x 2.x项目创建
  6. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)
  7. 向linux内核加入系统调用新老内核比較
  8. javascript中的undefined,null,&quot;&quot;,0和false的云集
  9. 【原】Spark中Client源码分析(二)
  10. Linux系统及应用问题分析排查工具
  11. 第 2 章 代理模式【Proxy Pattern】
  12. MySQL账号授权操作
  13. mongodb debug
  14. 奶瓶beini系统
  15. Nastya Is Buying Lunch
  16. P1605 迷宫
  17. java运算符和流程图
  18. 【Alpha阶段】测试报告
  19. VS2013打包程序步骤
  20. HTML和CSS的静态页面

热门文章

  1. hashlib库的使用
  2. mvc 页面简单get获取后台数据
  3. 04.Path类的学习
  4. vs.net打包(自动检测环境并安装.net framwork)
  5. javascript小数相减出现一长串的小数位数
  6. C#学习笔记10
  7. 重构指南 - 尽快返回(Return ASAP )
  8. java使用commons-fileupload进行文件上传
  9. 洛谷P1970 花匠(dp)
  10. easyui datagrid 获取行数据某个字段