父组件
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<!--<HelloWorld msg="Welcome to Your Vue.js App"/>-->
父组件的值<input type="text" v-model="parentVal">
<div>
<child-test :inputData="parentVal"></child-test>
</div>
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
import ChildTest from '@/components/child-test.vue'
export default {
name: 'home',
components: {
HelloWorld,ChildTest
},
data() {
return {
parentVal: 100,
}
},
}
</script>
子组件
<template>
<div class="child">
子组件<input type="text" v-model="childVal">
</div>
</template>
<script>
export default {
name: 'child',
props: {
inputData: {
}
},
data() {
return {
childVal: this.inputData
}
},
watch: {
inputData(newVal){
this.childVal = newVal;
}
}
}
</script>
子传父
子组件
methods:{
buttons(){
this.$emit('fangfa',1)
}
}
父组件
<child-test :v-on="buttons"></child-test>
buttons(value){
console.log(value)
}