基于VUE技术实现的父子组件之间的通信

效果图:

  • 改变前

  • 改变后

方式1: 父子组件之间的通信:基于props及事件派发的方式

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>01-父子组件之间的通信:基于props及事件派发的方式</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<p>父组件的内容:{{content}}</p>
		<me-button 
			v-bind:name="name" 
			v-on:change-parent-content="changeContentHandle">
		</me-button>
	</div>
	<script type="text/javascript">
		// 定义名为 me-button 的子组件
		Vue.component('me-button', {
			props: {
				name: {
					type: String
				}
			},
			template: '<button v-on:click="changeParentContentHandle">我是子组件的按钮, 父组件传递给子组件的数据是: {{name}},点击我改变父组件的内容</button>',
			methods: {
				changeParentContentHandle: function() {
					this.$emit('change-parent-content', '内容被子组件改变了')
				}
			}
		})

		// Vue初始化实例
		new Vue({
			el: '#app',
			data: {
				content: '我是父组件的内容',
				name: 'mj'
			},
			methods: {
				changeContentHandle: function(value) {
					this.content = value
				}
			}
		})
	</script>
</body>
</html>

方式2: 父子组件之间的通信:基于props及组件的双向数据绑定sync+update实现

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>02-父子组件之间的通信:基于props及组件的双向数据绑定sync+update实现</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<p>父组件的内容:{{content}}</p>
		<me-button 
			v-bind:name="name" 
			v-bind:content.sync="content">
		</me-button>
	</div>
	<script type="text/javascript">
		// 定义名为 me-button 的子组件
		Vue.component('me-button', {
			props: {
				name: {
					type: String
				},
				content: {
					type: String
				}
			},
			template: '<button v-on:click="changeParentContentHandle">我是子组件的按钮, 父组件传递给子组件的数据是: {{name}},点击我改变父组件的内容</button>',
			methods: {
				changeParentContentHandle: function() {
					this.$emit('update:content', '内容被子组件改变了')
				}
			}
		})

		// Vue初始化实例
		new Vue({
			el: '#app',
			data: {
				content: '我是父组件的内容',
				name: 'mj'
			}
		})
	</script>
</body>
</html>

方式3: 父子组件之间的通信:基于props及组件的双向数据绑定v-model实现

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>03-父子组件之间的通信:基于props及组件的双向数据绑定v-model实现</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<p>父组件的内容:{{content}}</p>
		<me-button 
			v-bind:name="name" 
			v-model="content">
		</me-button>
	</div>
	<script type="text/javascript">
		// 定义名为 me-button 的子组件
		Vue.component('me-button', {
			props: {
				name: {
					type: String
				},
				content: {
					type: String
				}
			},
			template: '<button v-on:click="changeParentContentHandle">我是子组件的按钮, 父组件传递给子组件的数据是: {{name}},点击我改变父组件的内容</button>',
			methods: {
				changeParentContentHandle: function() {
					this.$emit('input', '内容被子组件改变了')
				}
			}
		})

		// Vue初始化实例
		new Vue({
			el: '#app',
			data: {
				content: '我是父组件的内容',
				name: 'mj'
			}
		})
	</script>
</body>
</html>

方式4: 父子组件之间的通信:基于props及EventBus自定义派发事件技术实现

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>04-父子组件之间的通信:基于props及EventBus自定义派发事件技术实现</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<p>父组件的内容:{{content}}</p>
		<me-button 
			v-bind:name="name">
		</me-button>
	</div>
	<script type="text/javascript">
		// 定义名为 me-button 的子组件
		Vue.component('me-button', {
			props: {
				name: {
					type: String
				}
			},
			template: '<button v-on:click="changeParentContentHandle">我是子组件的按钮, 父组件传递给子组件的数据是: {{name}},点击我改变父组件的内容</button>',
			methods: {
				changeParentContentHandle: function() {
					this.$eventBus.$emit('chang-parent-content', '内容被子组件改变了')
				}
			}
		})

		// eventBus作为Vue插件
		Vue.use({
			install(Vue) {
				Vue.prototype.$eventBus = new Vue;
			}
		})

		// Vue初始化实例
		new Vue({
			el: '#app',
			data: {
				content: '我是父组件的内容',
				name: 'mj'
			},
			created: function() {
				this.$eventBus.$on('chang-parent-content', function(value) {
					this.content = value
				}.bind(this))
			}
		})
	</script>
</body>
</html>

方式5: 基于Vuex技术实现,未完待续。。。。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值