Vue 之 组件之间的传值通信(多种方法,简洁而不失重点)

一、数据的通信

A、父组件给子组件传值(子组件使用父组件的数据)

  父组件给子组件传值使用 props 接收;

父组件

<template>
  <div>
    <child :msg="message"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    
    components: {
    
    
      child
    },
    data() {
    
    
    	return {
    
    
			message: 'abc'
		}
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childProps()">点击</button>
  </div>
</template>
<script>
  export default {
    
    
    props: {
    
    
      msg: {
    
    
        type: String,
        require: false,
        default: ''
      }
    },
    methods: {
    
    
      childProps() {
    
    
        if (this.msg) {
    
    
          console.log(this.msg);
        }
      }
    }
  };
</script>


B、子组件给父组件传值(父组件使用子组件的数据)

方法1、子组件给父组件传值使用$emit;

父组件

<template>
  <div>
    <my-child @change='changeHandle'></my-child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    
    components: {
    
    
      my-child
    },
    data() {
    
    
    	return {
    
    
			
		}
    },
    methods: {
    
    
	    changeHandle(arguments){
    
    
	        console.log(arguments) // 1, 2, 3, 4, 5
	    }
	}
  };
</script>

子组件

<template>
  <div>
    <button @click="hadleClick()">点击</button>
  </div>
</template>
<script>
  export default {
    
    
    props: {
    
    
      msg: {
    
    
        type: String,
        require: false,
        default: ''
      }
    },
    methods: {
    
    
      hadleClick() {
    
    
        this.$emit('change', 1, 2, 3, 4, 5) // 参数1,使用调用子组件时定义的事件,参数2~max,需要传送的值
      }
    }
  };
</script>

方法2、直接使用 ref 获取;

父组件

<template>
  <div>
    <my-child ref='myChild'></my-child>
    <button @click='getMessage'>测试</button>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    
    components: {
    
    
      my-child
    },
    data() {
    
    
    	return {
    
    
			
		}
    },
    methods: {
    
    
	    getMessage(){
    
    
	        console.log(this.$refs.myChild.message) // asb
	    }
	}
  };
</script>

子组件

<template>
  <div>
    <button>测试</button>
  </div>
</template>
<script>
  export default {
    
    
    data() {
    
    
    	return {
    
    
			message: 'asb'
		}
    },
    methods: {
    
    
      
    }
  };
</script>

  

二、方法的调用

A、子组件调用父组件的方法

方法1、直接在子组件中通过this.$parent.event来调用父组件的方法

父组件

<template>
  <div>
    <child></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    
    components: {
    
    
      child
    },
    methods: {
    
    
      fatherMethod() {
    
    
        console.log('测试');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    
    
    methods: {
    
    
      childMethod() {
    
    
        this.$parent.fatherMethod();
      }
    }
  };
</script>


方法2、在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了


父组件

<template>
  <div>
    <child @test="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    
    components: {
    
    
      child
    },
    methods: {
    
    
      fatherMethod() {
    
    
        console.log('测试');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    
    
    methods: {
    
    
      childMethod() {
    
    
        this.$emit('test');
      }
    }
  };
</script>


方法3、父组件通过 props 把方法传入子组件中,在子组件里直接调用这个方法


父组件

<template>
  <div>
    <child :child="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    
    components: {
    
    
      child
    },
    methods: {
    
    
      fatherMethod() {
    
    
        console.log('测试');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    
    
    props: {
    
    
      child: {
    
    
        type: Function,
        default: null
      }
    },
    methods: {
    
    
      childMethod() {
    
    
        if (this.child) {
    
    
          this.child();
        }
      }
    }
  };
</script>


B、父组件使用子组件的方法

通过ref 直接调用子组件的方法


父组件

<template>
    <div>
        <Button @click="handleClick">点击调用子组件方法</Button>
        <Child ref="child"/>
    </div>
</template>    

<script>
import Child from './child';
export default {
    
    
    methods: {
    
    
        handleClick() {
    
    
              this.$refs.child.sing();
        },
    },
}
</script>

子组件

<template>
  <div>我是子组件</div>
</template>
<script>
export default {
    
    
  methods: {
    
    
    sing() {
    
    
      console.log('我是子组件的方法');
    },
  },
};
</script>

猜你喜欢

转载自blog.csdn.net/Zhuangvi/article/details/115874680
今日推荐