<div id="app">
<div class="p-5">
<h3>methods 的結構</h3>
<ul>
<li>由 methods 定義的物件</li>
<li>內層均是函式</li>
</ul>
<h3>methods 的觸發方法(點擊、其它 options api、生命週期...)</h3>
<button type="button" @click="trigger('Click Methods')">點擊觸發</button>
<button type="button" @click="callOtherMethod" class="ms-1">呼叫另一個 methods</button>
</div>
</div>
const App = {
data() {
return {
num: 10,
products: [
{
name: '蛋餅',
price: 30,
vegan: false
},
{
name: '飯糰',
price: 35,
vegan: false
},
{
name: '小籠包',
price: 60,
vegan: false
},
{
name: '蘿蔔糕',
price: 30,
vegan: true
},
],
carts: [],
sum: 0,
}
},
methods: {
trigger(name) {
console.log(name, '此事件被觸發了')
},
callOtherMethod() {
this.trigger('由 callOtherMethod 觸發')
},
methodParameter(a, b, c, d) {
console.log(a, b, c, d)
},
// addToCart(product) {
// this.carts.push(product)
// },
// calculate() {
// let total = 0;
// this.carts.forEach(item => {
// total += item.price;
// });
// this.sum = total;
// },
// convertToAmount(price) {
// return `NT$ ${price}`;
// }
},
created() {
this.trigger('由生命週期觸發');
}
};
Vue.createApp(App).mount('#app');