LaunchDarkly Vue Client SDK
The LaunchDarkly Vue Client SDK provides seamless integration of feature flags into Vue.js applications, specifically supporting Vue 3.2 and newer versions. As of `2.5.0`, it offers a robust solution for dynamically managing features, building upon the core LaunchDarkly JavaScript SDK. Key differentiators include first-class support for Vue's Composition API through reactive hooks (like `useFlags` and `useLDClient`) and a dedicated Vue plugin for easy initialization. The SDK sees frequent releases, typically every 1-3 months, ensuring compatibility with the latest Vue and LaunchDarkly platform features. It is designed for client-side environments, requiring a client-side ID for authentication and specific flag configurations to ensure availability.
Common errors
-
TypeError: app.use is not a function
cause Attempting to use the SDK's Vue plugin with a Vue 2 application or an improperly created Vue 3 app instance.fixEnsure your project is running Vue 3.2 or newer and that `app` is a valid Vue 3 application instance created with `createApp()`. -
LaunchDarkly client not initialized or flag not found (fallback value returned unexpectedly).
cause The LaunchDarkly client failed to initialize due to an invalid `clientSideId`, or the specific feature flag is not enabled for client-side SDKs in the LaunchDarkly dashboard.fixVerify that the `clientSideId` passed to `LDPlugin` is your correct LaunchDarkly Client-side ID (not an SDK key). Also, confirm that the feature flag in question has the 'Make this flag available to client-side SDKs' option checked in your LaunchDarkly dashboard. -
Property 'email' does not exist on type 'LDContext'.
cause Attempting to access a specific attribute like 'email' directly on a generic `LDContext` type without specifying its structure or `kind` property in TypeScript.fixWhen defining your `LDContext`, ensure you explicitly specify custom attributes or use type assertions if your context has a known structure. For example: `{ kind: 'user', key: 'example', email: 'test@example.com' } as LDContext & { email: string }`. -
Error: useLDFlag must be called from within a setup function or script setup.
cause The `useLDFlag` (or other `useLD...` composables) is being called outside of a Vue Composition API `setup` function or a `<script setup>` block.fixEnsure all LaunchDarkly composables (`useLDFlag`, `useFlags`, `useLDClient`, etc.) are invoked directly within the `<script setup>` block of a Vue component or inside the `setup()` method of an options API component.
Warnings
- gotcha The SDK requires a 'Client-side ID' for initialization, not an 'SDK key' or 'Mobile key'. Using an incorrect key type (e.g., a server-side SDK key) will prevent the client from initializing correctly and could lead to unexpected behavior or security issues.
- gotcha For any feature flag to be available to the Vue SDK, the 'Make this flag available to client-side SDKs' checkbox must be enabled on that flag's settings page in the LaunchDarkly dashboard. If not enabled, the SDK will receive the flag's fallback value.
- breaking The `waitForInitialization` method's behavior changed in version 2.2.0. If a timeout is specified, the returned promise will now be rejected after the timeout elapses if the client has not finished initializing. Previously, it would only resolve or reject upon completion or failure without a timeout.
- breaking The `track` method now validates that the `metricValue` parameter is a number since version 2.2.0. If a non-numeric value is provided for `metricValue`, a warning will be logged, and the event might not be tracked as expected.
- breaking This SDK exclusively supports Vue 3.2 and newer versions. Attempts to use it with Vue 2.x projects will result in compatibility errors and unexpected behavior.
- breaking Starting with version 2.0.0, the SDK only operates on 'contexts' instead of 'users'. Any existing code from 1.x versions that used `LDUser` must be updated to use `LDContext`. While the underlying JS SDK might convert `LDUser` to `LDContext` implicitly, it's strongly recommended to explicitly use `LDContext` for correct behavior and full feature access.
Install
-
npm install launchdarkly-vue-client-sdk -
yarn add launchdarkly-vue-client-sdk -
pnpm add launchdarkly-vue-client-sdk
Imports
- LDPlugin
import LaunchDarklyVue from 'launchdarkly-vue-client-sdk'
import { LDPlugin } from 'launchdarkly-vue-client-sdk' - useFlags
import { useFlags } from 'launchdarkly-vue-client-sdk' - useLDClient
import { useLDClient } from 'launchdarkly-vue-client-sdk' - useLDFlag
import { useLDFlag } from 'launchdarkly-vue-client-sdk' - LDContext
import { LDContext } from 'launchdarkly-vue-client-sdk'
Quickstart
import { createApp } from 'vue';
import App from './App.vue';
import { LDPlugin, LDContext, useLDFlag } from 'launchdarkly-vue-client-sdk';
// It's best practice to load your client-side ID from environment variables.
// For Vite, use import.meta.env.VITE_LD_CLIENT_SIDE_ID
// For other bundlers or Node.js, use process.env.LD_CLIENT_SIDE_ID
const clientSideId = process.env.VITE_LD_CLIENT_SIDE_ID ?? 'YOUR_CLIENT_SIDE_ID'; // Replace with your actual Client-side ID
const context: LDContext = {
kind: 'user',
key: 'example-user-key',
name: 'Example User',
// Custom attributes can be added here
_meta: { privateAttributes: ['email'] }, // Example: Mark 'email' as private
};
const app = createApp(App);
// Initialize the LaunchDarkly Vue plugin
app.use(LDPlugin, {
clientSideId,
context,
// Optional: Add further options for the underlying JS SDK
options: {
streaming: true, // Enables real-time flag updates
// logger: LaunchDarkly.basicLogger({ level: 'debug' }), // Uncomment for debug logs
},
});
app.mount('#app');
// App.vue
<script setup lang="ts">
import { ref, watchEffect } from 'vue';
import { useLDClient, useLDFlag } from 'launchdarkly-vue-client-sdk';
// Use useLDFlag for a specific flag, providing a fallback value
const myFeatureEnabled = useLDFlag('my-boolean-feature', false);
// Access the underlying LDClient for advanced operations like tracking events
const ldClient = useLDClient();
const trackCustomEvent = () => {
if (ldClient) {
ldClient.track('button-clicked', { customData: 'from-vue' });
console.log('Custom event tracked!');
}
};
</script>
<template>
<div>
<h1>LaunchDarkly Vue SDK Demo</h1>
<p>Is 'my-boolean-feature' enabled? <strong>{{ myFeatureEnabled ? 'Yes' : 'No' }}</strong></p>
<button @click="trackCustomEvent">Track Button Click</button>
<p v-if="myFeatureEnabled">This content is only visible if 'my-boolean-feature' is ON!</p>
<p v-else>This content is visible if 'my-boolean-feature' is OFF!</p>
</div>
</template>