利用vm.$listeners, vm.$attrs实现简易的隔代通讯

相关阅读:

<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>v-on="$listeners" v-bind="$attrs" v-bind="$props"</title>
		<style>
			.components {
				border: 1px solid #000;
				padding: 30px;
			}
		</style>
	</head>

	<body>

		<div id="swq">
			<box></box>
		</div>

		<script type="text/x-template" id="box-template">
			<div class="components">
				<div>box1:{{box1}}</div>
				<div>box2:{{box2}}</div>
				<c-a @onFunA1="funA1" @onFunA2="funA2" :box1="box1" :box2="box2"></c-a>
			</div>
		</script>
		<script type="text/x-template" id="cA-template">
			<div class="components">
				<div>box1:{{box1}}</div>
				<div @click="playFunA1">play funA1</div>
				<div @click="playFunA2">play funA2</div>
				<c-b v-on="$listeners" v-bind="$attrs"> </c-b>
			</div>
		</script>
		<script type="text/x-template" id="cB-template">
			<div class="components">
				<div>box2:{{box2}}</div>
				<div @click="playFunA1">play funA1</div>
				<div @click="playFunA2">play funA2</div>
				<c-c v-on="$listeners"></c-c>
			</div>
		</script>
		<script type="text/x-template" id="cC-template">
			<div class="components">
				<div @click="playFunA1">play funA1</div>
				<div @click="playFunA2">play funA2</div>
			</div>
		</script>

		<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
		<script type="text/javascript">
			var cC = {
				template: "#cC-template",
				//				inheritAttrs: false,
				methods: {
					playFunA1() {
						this.$emit("onFunA1");
					},
					playFunA2() {
						this.$emit("onFunA2");
					},
				},
			};
			var cB = {
				template: "#cB-template",
				props: ["box2"],
				//				inheritAttrs: false,
				components: {
					"c-c": cC,
				},
				methods: {
					playFunA1() {
						this.$emit("onFunA1");
					},
					playFunA2() {
						this.$emit("onFunA2");
					},
				},
			};
			var cA = {
				template: "#cA-template",
				props: ["box1"],
				components: {
					"c-b": cB,
				},
				methods: {
					playFunA1() {
						this.$emit("onFunA1");
					},
					playFunA2() {
						this.$emit("onFunA2");
					},
				},
			};
			var box = {
				template: "#box-template",
				data() {
					return {
						box1: 1,
						box2: 2,
					}
				},
				components: {
					"c-a": cA
				},
				methods: {
					funA1() {
						this.box1++
					},
					funA2() {
						this.box2++
					},
				},
			};
			var vu = new Vue({
				el: "#swq",
				components: {
					box: box
				},
			})
		</script>
	</body>

</html>

end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值