Skip to content

Commit

Permalink
feat(vue-demo): add defineExpose example
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqours committed Mar 29, 2024
1 parent 5412244 commit ff8637a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions apps/vue-demo/src/component/atom/CounterAtom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<script setup lang="ts">
import { onMounted, onUnmounted, reactive } from 'vue';
import { ICounterRef } from '../types';
let initValue = Math.round(Math.random() * 10);
let timer: number = -1;
Expand All @@ -15,6 +16,12 @@ onMounted(() => {
}, 1000);
});
defineExpose<ICounterRef>({
add(x: number) {
data.count += x;
},
});
onUnmounted(() => {
clearInterval(timer);
});
Expand Down
13 changes: 12 additions & 1 deletion apps/vue-demo/src/component/pinia/PiniaSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
import HeadingAtom from '../atom/HeadingAtom.vue';
import CounterAtom from '../atom/CounterAtom.vue';
import PiniaSubmit from './PiniaSubmit.vue';
import { ref } from 'vue';
import { ICounterRef } from '../types';
const counterRef = ref<ICounterRef>();
const onClick = () => {
if (counterRef.value) {
counterRef.value.add(100);
}
};
</script>

<template>
<HeadingAtom />
<CounterAtom />
<CounterAtom ref="counterRef" />
<button @click="onClick">+100</button>
<PiniaSubmit />
</template>
7 changes: 7 additions & 0 deletions apps/vue-demo/src/component/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Demo for shared types
*/

export interface ICounterRef {
add(x: number): void;
}

0 comments on commit ff8637a

Please sign in to comment.