<div id="root" >
父组件的值改变:{{num}}
<hello :num=num @add=funadd>
</div>
<script src="vue.js"></script>
<script>
var vm=new Vue({
el:'#root',
data:{
text:'hello',
text1:'隐藏',
num:1,
obj:{
name:'lingling'
},
bool:true,
arr:[
{name:'ling',
age:18},
{name:'wei',
age:36}
]
},
methods:{
funadd(mynum){
this.num=mynum;
}
},
components:{
hello:{
props:['num','obj'],
data(){
return {
mynum:this.num
}
},
template:`
<div>
<button @click=fun>click me </button>
<div > 子组件的值改变:{{mynum}}</div>
</div>
`,
methods:{
fun(){
// this.bool=!this.bool,
// this.text1=this.bool?'隐藏':'显示'
this.mynum+=10;
this.$emit('add',this.mynum)
}
}
}
},
})