9、vue2.0子组件触发父组件的方法,父组件接收子组件的方法

来源:互联网 发布:php短信轰炸机 编辑:程序博客网 时间:2024/05/29 18:21


方法一:

子组件:

复制代码
<template>    <button @click="submit">提交</button></template><script>export default {  methods: {    submit: function () {      // 子组件中触发父组件方法ee并传值cc12345      this.$emit('ee', 'cc12345')    }  }}</script>
复制代码

父组件:

复制代码
<template>    <editor id="editor" class="editor" @ee="cc"></editor></template><script>export default {  methods: {    cc: function (str) {      alert(str)    }  }}</script>
复制代码

 

方法二:

子组件:

复制代码
<template>    <button @click="submit">提交</button></template><script>export default {  props: {    onsubmit: {      type: Function,      default: null    }  },  methods: {    submit: function () {      if (this.onsubmit) {        this.onsubmit(‘cc12345’)      }    }  }}</script>
复制代码

父组件:

复制代码
<template>    <editor id="editor" class="editor" :onsubmit="cc"></editor></template><script>export default {  methods: {    cc: function (str) {      alert(str)    }  }}</script>