Vue - 子组件如何调用父组件中的方法

我不是女神ヾ 2021-07-26 00:33 1034阅读 0赞

子组件调用父组件的三种方法

    • 一. 子组件里用$emit向父组件触发一个事件,父组件监听这个事件
    • 二. 子组件通过使用this.$parent.event
    • 三. 父组件通过绑定方式把方法名传给子组件,在子组件里调用这个方法

一. 子组件里用$emit向父组件触发一个事件,父组件监听这个事件

父组件

  1. <template>
  2. <div>
  3. <el-button @click="show">展开子组件抽屉</el-button>
  4. <!-- 抽屉 -->
  5. <el-drawer
  6. title="我是标题"
  7. :visible.sync="drawerVisible"
  8. :with-header="false"
  9. :before-close="handleClose"
  10. >
  11. <!-- 子组件 -->
  12. <!-- 通过方法监听子组件的状态 -->
  13. <child @hideMethods="hideDrawer"></child>
  14. </el-drawer>
  15. </div>
  16. </template>
  17. <script>
  18. //子组件的地址(仅供参考),以项目实际目录为准
  19. import child from "../test/Child.vue";
  20. export default {
  21. components: {
  22. child: child //子组件
  23. },
  24. data() {
  25. return {
  26. drawerVisible: false, //控制抽屉显示与隐藏
  27. };
  28. },
  29. methods: {
  30. show() {
  31. //展开抽屉
  32. this.drawerVisible = true;
  33. },
  34. //darwer抽屉关闭前的回调,会暂停 Drawer 的关闭
  35. handleClose() {},
  36. //被子组件执行的方法,接收子组件传递的数据
  37. hideDrawer() {
  38. console.log("执行到了")
  39. //关闭抽屉
  40. this.drawerVisible = false;
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped>
  46. </style>

子组件

  1. <template>
  2. <div>
  3. <el-button @click="hideDra">关闭</el-button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {};
  10. },
  11. methods: {
  12. //执行关闭方法
  13. hideDra() {
  14. this.$emit("hideMethods");
  15. }
  16. }
  17. };
  18. </script>
  19. <style scoped>
  20. </style>

二. 子组件通过使用this.$parent.event

注意:有时候会失效,不建议使用。
父组件

  1. <template>
  2. <div>
  3. <span>这里是父组件</span>
  4. <el-divider></el-divider>
  5. <el-button @click="showDrawer">打开子组件</el-button>
  6. <!-- 抽屉 -->
  7. <el-drawer
  8. title="我是标题"
  9. :visible.sync="drawerVisible"
  10. :with-header="false"
  11. :before-close="handleClose"
  12. >
  13. <!-- 子组件 -->
  14. <child></child>
  15. </el-drawer>
  16. </div>
  17. </template>
  18. <script>
  19. //子组件的地址(仅供参考),以项目实际目录为准
  20. import child from "../test/Child.vue";
  21. export default {
  22. components: {
  23. child: child //子组件
  24. },
  25. data() {
  26. return {
  27. drawerVisible: false //控制抽屉显示与隐藏
  28. };
  29. },
  30. methods: {
  31. //展开抽屉
  32. showDrawer(row) {
  33. this.drawerVisible = true;
  34. },
  35. //darwer抽屉关闭前的回调,会暂停 Drawer 的关闭
  36. handleClose() {},
  37. //将要被子组件调用的方法
  38. hide(){
  39. console.log("执行到了")
  40. this.drawerVisible = false;
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped>
  46. </style>

子组件

  1. <template>
  2. <div>
  3. <span>这里是子组件</span>
  4. <el-divider></el-divider>
  5. <el-row>
  6. <el-button size="mini" @click="hideD">关闭</el-button>
  7. </el-row>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. methods: {
  13. //执行父组件中的关闭方法
  14. hideD() {
  15. this.$parent.hide();
  16. }
  17. }
  18. };
  19. </script>
  20. <style scoped>
  21. </style>

三. 父组件通过绑定方式把方法名传给子组件,在子组件里调用这个方法

父组件

  1. <template>
  2. <div>
  3. <span>这里是父组件</span>
  4. <el-divider></el-divider>
  5. <el-button @click="showDrawer">打开子组件</el-button>
  6. <!-- 抽屉 -->
  7. <el-drawer
  8. title="我是标题"
  9. :visible.sync="drawerVisible"
  10. :with-header="false"
  11. :before-close="handleClose"
  12. >
  13. <!-- 子组件 -->
  14. <child :hideDrawer="hideDrawer"></child>
  15. </el-drawer>
  16. </div>
  17. </template>
  18. <script>
  19. //子组件的地址(仅供参考),以项目实际目录为准
  20. import child from "../test/Child.vue";
  21. export default {
  22. components: {
  23. child: child //子组件
  24. },
  25. data() {
  26. return {
  27. drawerVisible: false //控制抽屉显示与隐藏
  28. };
  29. },
  30. methods: {
  31. //展开抽屉
  32. showDrawer(row) {
  33. this.drawerVisible = true;
  34. },
  35. //darwer抽屉关闭前的回调,会暂停 Drawer 的关闭
  36. handleClose() {},
  37. //将要被子组件调用的方法
  38. hideDrawer(){
  39. this.drawerVisible = false;
  40. console.log("执行到了")
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped>
  46. </style>

子组件

  1. <template>
  2. <div>
  3. <span>这里是子组件</span>
  4. <el-divider></el-divider>
  5. <el-row>
  6. <el-button size="mini" @click="hideD">关闭</el-button>
  7. </el-row>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props: {
  13. hideDrawer: {
  14. type: Function,
  15. default: null
  16. }
  17. },
  18. methods: {
  19. //执行父组件中的关闭方法
  20. hideD() {
  21. if(this.hideDrawer){
  22. this.hideDrawer()
  23. }
  24. }
  25. }
  26. };
  27. </script>
  28. <style scoped>
  29. </style>

发表评论

表情:
评论列表 (有 0 条评论,1034人围观)

还没有评论,来说两句吧...

相关阅读