You can listen to Telegram Mini App events using two primary methods: direct event-handler functions or Vue composables.
Method 1: Event-handler functions
Event-handler functions follow the naming convention on<EventName> in camelCase. For example, the themeChanged event is handled via onThemeChanged.
Method 2: Composables
Most events are also exposed via composables, which often provide an .onChange(), .onStart(), or similar method for a more reactive experience.
Managing Subscriptions
By default, event handlers automatically unsubscribe when the Vue component unmounts. If you need to manage the lifecycle manually, you can use the manual: true option or call .off() on the returned handler.
Warning: If you use { manual: true }, you are responsible for calling .off() to prevent memory leaks.
// Using event-handler functions
import { onThemeChanged } from 'vue-tg'
onThemeChanged(() => {
// handle theme update
})
// Using composables
import { useTheme } from 'vue-tg'
const theme = useTheme()
theme.onChange(() => {
// handle theme update
})
// Manual unsubscription
const handler = onThemeChanged(() => {}, { manual: true })
handler.off()