vue之prop,emit資料傳遞示例

Mars-xq發表於2019-01-08

在這裡插入圖片描述

parent.vue

<template>
  <div>
    父親: {{childInfo}}
    <child :parentInfo="parentMsg" @childInfo="handleChildMsg"/>
  </div>
</template>

<script>
  import child from "./ccc"

  export default {
    name: "parent",
    components: {
      child
    },
    data() {
      return {
        parentMsg: "來自父親的話",
        childInfo: ""
      }
    },
    methods: {
      handleChildMsg(data) {
        this.childInfo = data;
      }
    },
  }
</script>

<style scoped>

</style>

child.vue

<template>
  <div>
    兒子:{{parentInfo}}
    <button @click="sendMsg">給父親傳送訊息</button>
  </div>
</template>

<script>
  export default {
    name: "child",
    data() {
      return {
        childInfo: "兒子發來的訊息"
      }
    },
    // props: {//子接收資料
    //   parentInfo: {
    //     type: String,
    //     default: ""
    //   }
    // },
    props: ["parentInfo"],//子接收資料(簡寫)
    methods: {
      sendMsg() {
        this.$emit("childInfo", this.childInfo);
      }
    },
  }
</script>

<style scoped>

</style>


相關文章