首页 > 其他 > 详细

$emit(子组件给父组件传值)

时间:2020-03-29 13:26:37      阅读:90      评论:0      收藏:0      [点我收藏+]

App.vue

<template>
  <div id="app">
    <my-parent></my-parent>
  </div>
</template>

<script>
import MyParent from "./views/Parent";
export default {
  components: {
    MyParent
  }
};
</script>
<style>
</style>

Parent.vue

<template>
  <div>
    <h2>Parent--{{msg}}</h2>
    <my-child v-bind:msg="`from Parent`" @showMsg="showMsg1"></my-child>
  </div>
</template>

<script>
import MyChild from "./Child";
export default {
  components: {
    MyChild
  },
  data() {
    return {
      msg: ""
    };
  },
  methods: {
    showMsg1(val) {
      this.msg = val;
    }
  }
};
</script>

<style>
</style>

Child.vue

<template>
  <div>
    <h2>Child--{{msg}}</h2>
    <button @click="passMsg">给父组件传值</button>
  </div>
</template>

<script>
export default {
  props: {
    msg: {
      type: String,
      default: ""
    }
  },
  methods: {
    passMsg() {
      this.$emit("showMsg", "from Chlid");
    }
  }
};
</script>

<style>
</style>

$emit(子组件给父组件传值)

原文:https://www.cnblogs.com/xl4ng/p/12591658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!