Installation
Install package:
npm i vue-tg
To connect your Mini App to the Telegram client, place the script telegram-web-app.js
in the <head>
tag before any other scripts, using this code:
<script src="https://telegram.org/js/telegram-web-app.js"></script>
Done!
Global Aliases
Register on Vue instance:
import { VueTelegramPlugin } from 'vue-tg'
Vue.use(VueTelegramPlugin)
After that, you can use global aliases for components:
<!-- Without alias -->
<script lang="ts" setup>
import { Alert } from 'vue-tg'
</script>
<template>
<Alert message="Hello!" />
</template>
<!-- Same using an alias of the component -->
<template>
<tg-alert message="Hello!" />
</template>
Component | Alias |
---|---|
Alert | tg-alert |
BackButton | tg-back-button |
BiometricManager | tg-biometric-manager |
ClosingConfirmation | tg-closing-confirmation |
Confirm | tg-confirm |
ExpandedViewport | tg-expanded-viewport |
MainButton | tg-main-button |
Popup | tg-popup |
ScanQr | tg-scan-qr |
SettingsButton | tg-settings-button |
ShareWidget | tg-share-widget |
PostWidget | tg-post-widget |
LoginWidget | tg-login-widget |
DiscussionWidget | tg-discussion-widget |
Using with Nuxt 3
Install package:
npm i vue-tg
Include the Telegram web app script in the <head>
section of your Nuxt application by adding it to the nuxt.config.ts
file:
export default defineNuxtConfig({
app: {
head: {
script: [{ src: 'https://telegram.org/js/telegram-web-app.js' }],
},
},
})
Next, create a MiniApp.vue
component and import the necessary components from vue-tg
and utilize them in your component:
<script lang="ts" setup>
import { MainButton, useWebAppPopup } from 'vue-tg'
const { showAlert } = useWebAppPopup()
</script>
<template>
<MainButton text="Open alert" @click="() => showAlert('Hello!')" />
</template>
Import the MiniApp.vue
component into your App.vue
file and wrap it inside a <ClientOnly>
component. This ensures that the component is only rendered on the client-side:
<template>
<NuxtPage />
<ClientOnly>
<MiniApp />
</ClientOnly>
</template>
<script setup lang="ts">
import MiniApp from '~/components/MiniApp.vue'
</script>
You can manage the state within the MiniApp.vue
component using Nuxt's built-in useState
or any other state management library like pinia
.