父组件向子组件传值

 父组件向子组件传值

1、父组件传

父组件:
<template>
  <div>
    <input type="text" v-model="name">
    <br>
    <br>
    <!-- 子组件 -->
    <child :inputName="name"></child>
  </div>
</template>
<script>
  import child from './child'
  export default {
    components: {
      child
    },
    data () {
      return {
        name: ''
      }
    }
  }
</script>

2、子组件接收

<template>
  <div>
    子组件:
    <span>{{inputName}}</span>
  </div>
</template>
<script>
  export default {
    // 接受父组件的值
    props: {
      inputName: String,
      required: true
    },
    watch:{
	   inputName:{ 
	       handler:function(val){
			    console.log(val)       
	       },
	       deep:true
	    } 
	 },
     mounted(){
         console.log(this.inputName)
     }
  }
</script>

猜你喜欢

转载自blog.csdn.net/qq_36587340/article/details/82179472