UI frameworks
Vue
Mount a Vela Pro workspace in a Vue 3 Composition API component, watch reactive props, and avoid proxy wrapping.
Vela's open-source core and Vela Pro mount directly into a template element in Vue 3. In <script setup lang="ts">, construct VelaWorkspace inside onMounted, store the workspace in a plain let variable rather than ref(), watch props to update the chart in place, and call workspace.destroy() in onBeforeUnmount.
Single-File Component
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { VelaWorkspace } from '@luxalgo/vela/workspace';
import { BinanceProvider } from '@luxalgo/vela/providers/binance';
import { enableVelaPro } from '@luxalgo/vela-pro';
const props = withDefaults(defineProps<{ symbol?: string }>(), {
symbol: 'BTCUSDT',
});
enableVelaPro();
const container = ref<HTMLDivElement | null>(null);
let workspace: VelaWorkspace | undefined;
onMounted(() => {
workspace = new VelaWorkspace(container.value!, {
layout: false,
symbol: props.symbol,
timeframe: '60',
live: true,
providers: { binance: () => new BinanceProvider() },
});
});
watch(
() => props.symbol,
(nextSymbol) => {
if (workspace && workspace.active.symbol !== nextSymbol) {
workspace.active.setSymbol(nextSymbol);
}
},
);
onBeforeUnmount(() => {
workspace?.destroy();
workspace = undefined;
});
</script>
<template>
<div ref="container" style="width: 100%; height: 100%"></div>
</template>Implementation notes
- Store
VelaWorkspacein a plainletvariable: Do not wrapVelaWorkspaceinref()orreactive().VelaWorkspaceis an imperative class instance managing DOM nodes, WebGL2 contexts, and Web Workers rather than reactive template state, and Vue's deepProxywrapping interferes with internal object identity and adds tracking overhead. If you expose the workspace throughdefineExpose, useshallowRefor a getter function. - Sync reactive props with
watch(): InstantiateVelaWorkspaceonce inonMountedand apply subsequentsymbol,timeframe, orthemechanges viawatch()so user drawings, active indicators, and open panels remain intact. - Calling
enableVelaPro()in<script setup>: Code inside<script setup>runs each time a component instance initializes. BecauseenableVelaPro()is idempotent after the first call, calling it in<script setup>(or once inmain.ts) is safe as long as it runs beforenew VelaWorkspace(). - Clean up on unmount: Calling
workspace?.destroy()inonBeforeUnmountflushes pending state changes, terminates workers and live subscriptions, and removes the workspace DOM fromcontainer. Give the parent element a non-zero height so the100%container has space to fill.
Verified environment
| Stack | Verified versions | Checks run |
|---|---|---|
| Vue + Vite | Vue 3.5, Vite 8.3 | Typechecking with vue-tsc and production build (vite build) |