Vue SDK reference

Overview

This topic documents how to get started with the Vue SDK.

SDK quick links

LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositoryvue-client-sdk
Sample applicationVue
Published modulenpm

Get started

After you complete the Get started process, follow these instructions to start using the LaunchDarkly SDK in your Vue project:

Understand version compatibility

The LaunchDarkly Vue SDK only works with Vue 3:

  • Vue SDK 2.0 requires Vue 3.2 or newer
  • Vue SDK 1.x requires Vue 3 or newer

For Vue 2 projects, you can use the JavaScript SDK directly, or a community-developed package such as vue-ld.

The Vue SDK is based on the JavaScript SDK. The Vue SDK builds on LaunchDarkly’s JavaScript SDK to provide a better integration for use in Vue applications. As a result, much of the JavaScript SDK functionality is also available for the Vue SDK to use. For a complete client-side JavaScript SDK reference, read JavaScript SDK reference.

Install the SDK

First, install the Vue SDK.

We recommend making the LaunchDarkly observability plugins available as well. These plugins collect and send observability data to LaunchDarkly, including metrics autogenerated from observability events. This means you can review session replay, error monitoring, logs, and traces from within the LaunchDarkly UI. They require the Vue SDK version 2.4 or later.

Install the Vue SDK using either npm or yarn:

$npm install --save launchdarkly-vue-client-sdk
>npm install @launchdarkly/observability # optional observability plugin
>npm install @launchdarkly/session-replay # optional session replay plugin

Configure the SDK

The SDK provides a Vue plugin that you can add to your app. After you install the SDK, add the LaunchDarkly plugin to your Vue app. Typically you do this in main.js.

To configure the Vue SDK, you need your LaunchDarkly environment’s client-side ID. This authorizes your application to connect to a particular environment within LaunchDarkly.

The Vue SDK uses a client-side ID

The Vue SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from Project settings, on the Environments list. To learn more about key types, read Keys.

Client-side IDs are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.

Here’s how:

main.js
1import { createApp } from 'vue'
2import App from './App.vue'
3import { LDPlugin } from 'launchdarkly-vue-client-sdk'
4
5const clientSideID = 'client-side-id-123abc'
6
7const app = createApp(App)
8app.use(LDPlugin, { clientSideID })
9app.mount('#app')

You can pass configuration options to the plugin when it loads, or to the ldInit function if you are using the deferInitialization option. To learn more, read Initialize the client and context, below.

The plugin exposes the LaunchDarkly client, as well as some convenience functions. The plugin uses the Vue provide/inject API to do this, which means these convenience functions will only work when run within Vue’s setup hook or <script setup>. To learn more, read Provide/Inject.

Initialize the client and context

After you configure the Vue SDK and plugin, configure and initialize the LaunchDarkly client and the context. The context provides information about the end user who is encountering feature flags in your application.

The SDK automatically initializes the LaunchDarkly client if you provide a clientSideID and do not use the deferInitialization option. The client begins attempting to connect to LaunchDarkly as soon as it is created. To determine when it is ready to use, we strongly recommend calling waitForInitialization(). This method takes a timeout, which we recommend setting to five seconds or fewer.

If you do not know the context at plugin load time and would like to defer initialization until you do, you can use the deferInitialization option. In this case, you will need to initialize the LaunchDarkly client manually with ldInit, which accepts the same options as the plugin.

We recommend making the LaunchDarkly observability plugins available as well, as shown in the configuration options below.

Here’s how to initialize the client and verify it is ready to use:

1import { createApp } from 'vue'
2import App from './App.vue'
3import { LDPlugin } from 'launchdarkly-vue-client-sdk'
4import Observability from '@launchdarkly/observability'
5import { SessionReplay, LDRecord } from '@launchdarkly/session-replay'
6
7const app = createApp(App)
8app.use(LDPlugin, {
9 clientSideID: 'client-side-id-123abc',
10 deferInitialization: true,
11 options: {
12 plugins: [
13 new Observability(),
14 new SessionReplay()
15 ]
16 }
17})
18app.mount('#app')

To learn more about the available configuration options, read LDPluginOptions. To learn more about initialization, read ldInit and waitForInitialization.

Determine when the client is ready

The client begins attempting to connect to LaunchDarkly as soon as it is created. Then, you must check that it is ready to use. If you do not confirm this, your application may wait indefinitely if LaunchDarkly is unavailable.

To find out when the client has finished initializing, use ldReady. ldReady is a ref, so to access its value outside of a template you need to use ldReady.value.

ldReady is returned by ldInit. Alternatively, you can retrieve ldReady independently using useLDReady.

Here’s how:

Vue
1<script setup lang="ts">
2 import { useLDReady } from 'launchdarkly-vue-client-sdk'
3
4 const ldReady = useLDReady()
5</script>
6
7<template>
8 <div v-if="ldReady">... content that uses LaunchDarkly ...</div>
9 <div v-else>LaunchDarkly client initializing...</div>
10</template>

Retrieve flag values for the context

Use useLDFlag to access a single flag value by key. It returns a readonly ref for the value of a flag. If you are using TypeScript, the ref’s type can be inferred from the type of the fallback value provided to useLDFlag. To learn more about Vue refs, read ref in the Vue.js documentation.

Here’s how:

Vue
1<script setup lang="ts">
2 import { useLDFlag } from 'launchdarkly-vue-client-sdk'
3 const featureFlagKey = 'my-boolean-flag'
4 const myFlagValue = useLDFlag(featureFlagKey, false /* fallback flag value */)
5</script>
6
7<template>
8 Feature flag "{{ featureFlagKey }}" has value "{{ myFlagValue }}".
9</template>

The Vue SDK automatically subscribes to flag change events with useLDFlag, which opens a streaming connection. Then, your component re-renders automatically when flag changes occur.

In some cases, streaming may not be necessary. For example, if you reload your entire application on each update, you will get all the flag values again when the client is re-initialized. If you do not want flag updates streamed to your application, use the streaming: false option when you load the plugin or when you call ldInit.

In other cases, streaming may be required. If you call useLDFlag, the SDK automatically opens a streaming connection. This is a benefit of useLDFlag as compared with getting flag values using the underlying JavaScript SDK, for example with ldClient.variant() or ldClient.allFlags().

To learn more, read useLDFlag.

Making feature flags available to this SDK

You must make feature flags available to client-side SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the end user will receive the fallback value for that flag.

To make a flag available to this SDK, check the SDKs using Client-side ID checkbox during flag creation, or on the flag’s settings page. To make all of a project’s flags available to this SDK by default, check the SDKs using Client-side ID checkbox on your project’s Flag settings page.

Access the underlying JavaScript SDK

You may also access the LaunchDarkly client with useLDClient, the LDClient object from the underlying JavaScript SDK.

Here is an example:

JavaScript
1<script setup>
2import { useLDClient } from 'launchdarkly-vue-client-sdk'
3
4const [ldReady] = ldInit({ clientSideID, context })
5const ldClient = useLDClient()
6</script>
7
8<template>
9 <div v-if="ldReady">
10 <p>All flags: {{ JSON.stringify(ldClient.allFlags()) }}</p>
11 </div>
12 <div v-else>LaunchDarkly client initializing...</div>
13</template>

Example app

An example app is included in launchdarkly/vue-client-sdk.

Troubleshooting

If your application logs show the error LaunchDarklyFlagFetchError: network error, it may indicate a problem with network connectivity between your SDK and LaunchDarkly.

For steps to resolve this issue, read the LaunchDarkly Knowledge Base article Error “LaunchDarklyFlagFetchError: network error”.