<!-- 受限於codepen環境關係,vue warning無法使用
console觀看-->
<div id="app">
<div class="p-5">
<h3>Emits API</h3>
{{ num }}
<button-counter @add="addNum"></button-counter>
</div>
</div>
const app = Vue.createApp({
data() {
return {
num: 0,
};
},
methods: {
addNum(num) {
this.num = this.num + num;
},
}
});
app.component('button-counter', {
data() {
return {
num: 1,
}
},
emits:['add'],
// 主要會出現在該值是由 data 定義,但難以追蹤他的變化時會出現
template: `
<button type="button" @click="num++">調整 num 的值</button>
<button type="button" @click="$emit('add',num)">add</button>
`,
});
app.mount('#app');