需求:
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.
- Props
送參數給 component (send data from the parent element to the child component)
- $emit()
從 componet 取資訊 (pass information from the child component to the parent)
做法當然就是
- component 發出 event
例如:
this.$emit('toggle-Favorite');
- parent 接收 event
@toggle-favorite="receiveEmit"
methods: {
receiveEmit() {
alert("Hello world!")
}
}
為什麼用 @ 呢?
這本來就是 Vue「聽」event 的語法 v-on 的簡寫 (shorthand)。
後面自然是寫 JavaScript,
最簡單就是呼叫 function,就可以用參數帶 data 給 parent 囉。