Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This topic documents how to get started with the Vue SDK.LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
| Resource | Location |
|---|---|
| SDK API documentation | SDK API docs |
| Supported SDK Versions | Vue SDK |
| GitHub repository | vue-client-sdk |
| Sample application | Vue |
| Published module | npm |
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
- Install the SDK
- Configure the SDK
- Initialize the client and context
- Retrieve flag values for the context
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
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 eithernpm or yarn:
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 inmain.js.
To configure the Vue SDK, you need your LaunchDarkly environment’s client-side ID and the context for which you want to evaluate flags. This authorizes your application to connect to a particular environment within LaunchDarkly.
Here’s how:
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 aclientSideID 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:
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, useldReady. 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:
Retrieve flag values for the context
UseuseLDFlag 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:
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.
Access the underlying Java
Script SDK You may also access the LaunchDarkly client withuseLDClient, the LDClient object from the underlying JavaScript SDK.
Here is an example:
Example app
An example app is included in launchdarkly/vue-client-sdk.Troubleshooting
If your application logs show the errorLaunchDarklyFlagFetchError: 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”.