需求:
send information from the component to the parent,

With the built-in $emit() method
we can create a custom event in the child component that can be captured in the parent element.

  1. Props
    送參數給 component (send data from the parent element to the child component)

  2. $emit()
    從 componet 取資訊  (pass information from the child component to the parent)


做法當然就是
  1. component 發出 event
    例如:
    this.$emit('toggle-Favorite');

  2. parent 接收 event
    @toggle-favorite="receiveEmit"
    methods: {
        receiveEmit() {
            alert("Hello world!")
        }
    }

為什麼用 @ 呢?
這本來就是 Vue「聽」event 的語法 v-on 的簡寫 (shorthand)。

後面自然是寫 JavaScript,
最簡單就是呼叫 function,就可以用參數帶 data 給 parent 囉。
    評語
    請登入後才可以評分
    位置
    資料夾名稱
    Vue.js 學習筆記
    上傳者
    蘇德宙
    單位
    台灣數位員工
    建立
    2025-08-25 06:42:11
    最近修訂
    2025-08-25 06:59:20