HTML代码:
<body>
<div id="app">
<cpn :number1="num1"
:number2="num2"
@numinput1change="numinput1changes"
@numinput2change="numinput2changes"></cpn>
</div>
</body>
<template id="cpn">
<div>
<h2>props:{{number1}}</h2>
<h2>data:{{dnumber1}}</h2>
<!-- <input type="text" v-model="dnumber1">-->
<input type="text" :value="dnumber1" @input="numInput1">
<h2>props:{{number2}}</h2>
<h2>data:{{dnumber2}}</h2>
<!-- <input type="text" v-model="dnumber2">-->
<input type="text" :value="dnumber2" @input="numInput2">
</div>
</template>
JavaScript代码:
const app = new Vue({
el: '#app',
data: {
num1: 1,
num2: 0
},
components: {
cpn: {
template: '#cpn',
props: {
number1: Number,
number2: Number
},
data() {
return {
dnumber1: this.number1,
dnumber2: this.number2
}
},
methods: {
numInput1(event) {
this.dnumber1 = event.target.value
this.$emit('numinput1change', this.dnumber1)
},
numInput2(event) {
this.dnumber2 = event.target.value
this.$emit('numinput2change', this.dnumber2)
}
}
}
},
methods: {
numinput1changes(value) {
this.num1 = parseInt(value)
},
numinput2changes(value) {
this.num2 = value * 1
}
}
})