<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="div">
<h1>父组件的值{{num1}}</h1>
<cpn :number1="num1" @return="creturn"></cpn>
</div>
</body>
</html>
<template id="cpn">
<div>
<h1>子组件的值(获取了父组件的值):{{number1}}</h1>
<h1>子组件data(返回props的值): {{dnumber1}}</h1>
<input type="text" v-model="dnumber1" >
</div>
</template>
<script src="vue.js"></script>
<script>
const cpn={
template:"#cpn",
props:{
number1:String,
},
data(){
return {
dnumber1:this.number1,
}
},
watch:{
dnumber1(newValue){
this.dnumber1=newValue,
this.$emit("return",newValue)
}
}
}
const app=new Vue({
el:"#div",
data:{
num1:'1',
},
components:{
//注册子组件
cpn
},
methods:{
//this.dnumber1返回回来的是value
creturn(newValue){
this.num1=newValue
}
}
})
</script>
利用v-model进行双向绑定再用watch(当里面的元素发生改变时所执行下面的函数)监听元素的改变进行发射和赋值