vue父子组件通讯

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 
</head>
<body>
	<div id="app">
		<v :msgg="msg" :content="content" @itemclick="active"></v>
	</div> 
		
<template id="demo">
	<div>
		<button @click="btnClick(item)" v-for="item in group">{{item.title}}</button>
		<p>{{msgg}}</p>
		<vv :content="content"></vv>
	</div>
</template>

<template id="acticle">
	<div>
	
		<p>{{content}}</p>
	</div>
</template>


</body>

<script>
	const vv = {
	  template: '#acticle',
	  props: ['content'],
	}
	
	const v = {
	  template: '#demo',
	   props: ['msgg',"content"],
	  data(){
		  return {
			  group:[
				  {id:1,author:"刘禹锡",title:"秋词",text:"自古逢秋悲寂寥,我言秋日胜春朝"},
				  {id:2,author:"纳兰容若",title:"木兰花令拟古决绝词",text:"人生若只如初见,何事秋风悲画扇"}, 
				  {id:3,author:"辛弃疾",title:"鹧鸪天",text:"若教眼底无离恨,不信人间有白头"}, 
			  ],
			
		  }
	  },
	  methods:{
		  btnClick(item) {
			  this.$emit('itemclick',item)//事件发射    v-on 来监听 
		  }
	  },
	  components : {
	   	vv:vv,
	  }
	}
	
	 var vm = new Vue({
	  el: '#app',
	  data:{
		 bookTables:[
			{name:"php",price:30.00,count:0},	
			{name:"mysql",price:40.00,count:0},
			{name:"java",price:50.00,count:0},
			{name:"c++",price:60.00,count:0},
		 ],
		 content:'',
		 msg:"诗词"
	  },
	  methods:{
		  active(item){
			this.content = item.text
			console.log(this.content,"active")
		  }
		
	  },
	 components:{
	 	v:v
	 }
	})
	
	
</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值