Skip to content

Commit 385801a

Browse files
committed
Add emit-based increments to CounterStateless component
Refactored CounterStateless to emit custom events with increment values on button click. Updated MultipleCounter to handle these events and adjust the counter accordingly, enabling more flexible and dynamic updates.
1 parent 7b02a77 commit 385801a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/components/CounterStateless.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
<script setup>
22
3-
const {counter, name} = defineProps(["counter", "name"])
3+
const {counter, name, increment} = defineProps({
4+
counter: Number,
5+
name: String,
6+
increment: Number
7+
})
8+
const emits = defineEmits(["click"]);
9+
10+
function buttonClick() {
11+
emits("click", increment);
12+
}
413
514
</script>
615

716
<template>
8-
<h1>Counter {{name}} : {{ counter}}</h1>
17+
<div>
18+
<h1>Counter {{ name }} : {{ counter }}</h1>
19+
<button @click="buttonClick">Increment {{ name }}</button>
20+
</div>
921
</template>
1022

1123
<style scoped>

src/components/MultipleCounter.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ const counter2 = "Budi";
99
1010
const counter = ref(0);
1111
12+
function increment(value){
13+
counter.value += value;
14+
}
15+
1216
</script>
1317

1418
<template>
1519
<Counter :name="counter1" initial-count="10"/>
1620
<Counter v-bind:name="counter2" initial-count="20"/>
1721

18-
<CounterStateless name="Joko" :counter="counter"/>
19-
<CounterStateless name="Morro" :counter="counter"/>
20-
<button @click="counter++">Increment Counter</button>
22+
<CounterStateless name="Joko" :counter="counter" :increment="1" v-on:click="increment"/>
23+
<CounterStateless name="Morro" :counter="counter" :increment="2" @click="increment"/>
2124
</template>
2225

2326
<style scoped>

0 commit comments

Comments
 (0)