首页 > 编程知识 正文

vuejs父组件调用子组件,vue父组件调用子组件方法返回值

时间:2023-05-04 20:36:13 阅读:196184 作者:3324

1、页面中有一个对话框,是作为父组件,父组件有 “确定” 和 “取消” 两个按钮2、对话框中的表单是通过子组件来展示的,点击父组件的按钮,需要调用子组件中的方法。 通过 $refs 来调用子组件方法 父组件<template> <div> <button @click="parentClick">我是父组件的按钮</button> <child ref="is_child"></child> //在子组件上绑定ref属性 </div></template><script>import Child from "./child.vue";export default { components: { Child }, methods: { parentClick() { console.log(this.$refs.is_child); //返回的是一个vue实例对象,因此可以直接调用方法 this.$refs.is_child.sing(); //父组件中直接调用 $refs } }};</script> ```javascript子组件<template> <div></div></template><script>export default { data() { return { name: "tom" }; }, methods: { sing() { console.log("我是子组件的方法"); } }};</script>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。