Framework Integration
Embed in Vue
Show a Databrain dashboard or metric in a Vue 3 app.
Before you start
- Your backend returns a guest token at
/api/databrain-token. Create a guest token - You have a dashboard id. Find the dashboard id
Steps
- Install the plugin.
npm install @databrainhq/plugin- Tell Vue that
dbn-tags are custom elements, invite.config.ts. Without this, Vue warns “Failed to resolve component”.
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
vue({
template: {
compilerOptions: {
isCustomElement: (tag) => tag.startsWith('dbn-'),
},
},
}),
],
});- Import the plugin once in
src/main.ts, beforecreateApp.
import '@databrainhq/plugin/web';
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app');- Add a component that gets the token and renders
<dbn-dashboard>.
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';
const props = defineProps<{ dashboardId: string }>();
const token = ref<string>();
const el = ref<HTMLElement & { handleServerEvent?: (...args: unknown[]) => unknown }>();
const options = { disableMetricCreation: true, hideDashboardName: true };
onMounted(async () => {
const res = await fetch('/api/databrain-token');
token.value = (await res.json()).token;
});
watch(el, (dashboard) => {
if (dashboard) dashboard.handleServerEvent = (event) => console.log(event);
});
</script>
<template>
<dbn-dashboard
v-if="token"
:key="props.dashboardId"
ref="el"
:token="token"
:dashboard-id="props.dashboardId"
enable-download-csv="true"
:options="options"
></dbn-dashboard>
</template>You see the dashboard in your app.
Show one metric
<dbn-metric v-if="token" :key="metricId" :token="token" :metric-id="metricId"></dbn-metric>How the props work
- Strings and booleans go in the template as kebab-case attributes. Write booleans as
"true". - Vue sets an object, such as
:options, as a property on the element. - Functions go on the element through the template ref. Assign an inline arrow function. Do not bind functions in the template.
:keyremounts the element when the dashboard changes.
If something goes wrong
- Vue warns “Failed to resolve component: dbn-dashboard”. Add
isCustomElementas in step 2, then restart the dev server. - The embed shows the first dashboard of the data app, not yours. Bind the attribute as
:dashboard-id, in kebab case. Use the id, not the dashboard name. handleServerEventnever runs. Assign an inline arrow function. The element drops a named function.