1.通过sync修饰符配合$emit,update实现
父组件:
<template><child-view :num.sync="num"></child-view>
</template>
<script>import childView from './assembly/child'export default {components: {childView},data() {return {num: 2}}}</script>
子组件:
<template><div><p>父传过来的值:{{num}}</p><button @click="changeNum">加一</button></div>
</template><script>export default {name: 'child',props:{num: {type:Number,default: 0}},methods:{changeNum(){this.$emit("update:num",this.num + 1)}}}
</script>
2.通过v-model绑定数据源
父组件:
<template><child-view v-modal="num"></child-view>
</template>
<script>import childView from './assembly/child'export default {components: {childView},data() {return {num: 2}}}</script>
子组件:
<template><div><p>父传过来的值:{{num}}</p><input type="text" :value="value" @input="$emit('input',$event.target.value)"></div>
</template><script>export default {name: 'child',props:["value"]}
</script>
3.通过父传子,子调用父方法传参数实现
父组件:
<template><child-view :num="num" @updateNum="updateNum"></child-view>
</template>
<script>import childView from './assembly/child'export default {components: {childView},data() {return {num: 2}},methods: {updateNum(num){this.num = num}}</script>
子组件
<template><div><p>父传过来的值:{{num}}</p><button @click="changeNum">加一</button></div>
</template><script>export default {name: 'child',props:{num: {type:Number,default: 0}},methods:{changeNum(){this.$emit("updateNum",this.num + 1)}}}