Skip to Content

Framework Integration

Embed in Vue

Show a Databrain dashboard or metric in a Vue 3 app.

Before you start

Steps

  1. Install the plugin.
npm install @databrainhq/plugin
  1. Tell Vue that dbn- tags are custom elements, in vite.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-'),
        },
      },
    }),
  ],
});
  1. Import the plugin once in src/main.ts, before createApp.
import '@databrainhq/plugin/web';
import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');
  1. 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>

Find the metric id

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.
  • :key remounts the element when the dashboard changes.

If something goes wrong

  • Vue warns “Failed to resolve component: dbn-dashboard”. Add isCustomElement as 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.
  • handleServerEvent never runs. Assign an inline arrow function. The element drops a named function.