美文网首页
Vue - 子组件调用父组件的几种方法

Vue - 子组件调用父组件的几种方法

作者: 我常常问自己我是谁 | 来源:发表于2019-04-28 14:47 被阅读0次

参考:

www.cnblogs.com/jin-zhe/p/9523782.html#commentform 

这篇文章中介绍了三种方法分别为:
1、子组件通过使用this.parent.xxx (xxx:是你要调用的方法名) 2、子组件通过使用this.emit('xxx') (xxx:是你要调用的方法名,只要方法名不要后面的小括号)
3、父组件把方法名传给子组件,在子组件里调用这个方法

代码如下:

1、子组件通过使用this.$parent.xxx
父组件代码:

<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、子组件通过使用this.$emit('xxx')
父组件代码:

<template>
  <div>
//导入子组件名称标签,绑定方法名
    <child @fatherMethod="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('fatherMethod');
      }
    }
  };
</script>

3、父组件把方法名传给子组件,在子组件里调用这个方法
父组件代码:

<template>
  <div>
//导入子组件名称标签,并把方法名传给子组件
    <child :fatherMethod="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
    props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },
    methods: {
      childMethod() {
      //调用父组件的方法
        if (this.fatherMethod) {
          this.fatherMethod();
        }
      }
    }
  };
</script>

PS:三种方法,只有第一种方法是在路由下使用,使用时可以不用在父组件中间写 “<child></child>” 、 “import child from '~/components/dam/child';” 和 “components: {child },” ,第一个“<child></child>”可以直接不用写,后面两个在使用路由情况下,会在路由里上;
后面两种方法在使用路由情况下未能触发父组件的方法(也许是我写的方法不对,大家直接可以尝试一下)。
还有就是“<child></child>”这个不是标准的标签,同时它也是子组件的名字,也就是 child.vue (解释的不够标准,请见谅!)

注:如果有写的不对的地方或者侵犯版权请及时给我留言,我会第一时间更改或删除,谢谢!

相关文章

  • vue.js 核心知识点三

    目录 - 3.1 vue中子组件调用父组件的方法 - 3.2 Vue父组件调用子组件的方法 - 3.3 涉及到组件...

  • vue 父组件调用子组件的方法

    父组件可以通过this.$refs调用子组件的方法 父级.vue 子级 child.vue

  • vue父组件调用子组件的方法

    vue组件与组件通信有如下几种情况: 平行组件父组件与子组件子组件与父组件 它们之间通信有几种方法有: props...

  • Uni-App 父类和组件互相调用方法和传参

    父组件调用子组件方法 子组件调用父组件方法

  • 关于$refs的用法

    $refs父组件调用子组件的方法,可以传递数据。 示例 父组件 $refsFa.vue 子组件 emitCh.vu...

  • 组件之间的传值

    父组件调用子组件的方法: 子组件调用父组件或父页面的方法

  • 小程序父、子组件之间方法调用

    一:父组件调用子组件的方法 父组件.wxml 父组件.js 子组件.js 一:子组件调用父组件的方法 父组件.wx...

  • vue组件间参数与事件传递

    父组件向子组件传值 以及父组件调用子组件方法 子组件向父组件传值 以及子组件触发调用父组件方法

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

网友评论

      本文标题:Vue - 子组件调用父组件的几种方法

      本文链接:https://www.haomeiwen.com/subject/uyxdnqtx.html