Vue SDK

View as Markdown

The Unleash Vue SDK lets you evaluate feature flags in Vue applications. It connects to Unleash or Unleash Edge to fetch evaluated flags for a given Unleash context.

You can use this SDK with Unleash Enterprise or Unleash Open Source.

For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.

Installation

$npm install @unleash/proxy-client-vue

Initialization

Using config

1import { createApp } from 'vue'
2import { plugin as unleashPlugin } from '@unleash/proxy-client-vue'
3// import the root component App from a single-file component.
4import App from './App.vue'
5
6const config = {
7 url: 'https://HOSTNAME/api/frontend',
8 clientKey: 'FRONTEND_TOKEN',
9 refreshInterval: 15,
10 appName: 'your-app-name',
11}
12
13const app = createApp(App)
14app.use(unleashPlugin, { config })
15app.mount('#app')

Or use the FlagProvider component like this in your entrypoint file (typically App.vue):

1import { FlagProvider } from '@unleash/proxy-client-vue'
2
3const config = {
4 url: 'https://UNLEASH-INSTANCE/api/frontend',
5 clientKey: 'CLIENT-SIDE-API-TOKEN',
6 refreshInterval: 15,
7 appName: 'your-app-name',
8}
9
10<template>
11 <FlagProvider :config="config">
12 <App />
13 </FlagProvider>
14</template>

Initializing your own client

1import { createApp } from 'vue'
2import { plugin as unleashPlugin } from '@unleash/proxy-client-vue'
3// import the root component App from a single-file component.
4import App from './App.vue'
5
6const config = {
7 url: 'https://HOSTNAME/api/frontend',
8 clientKey: 'FRONTEND_TOKEN',
9 refreshInterval: 15,
10 appName: 'your-app-name',
11}
12
13const client = new UnleashClient(config)
14
15const app = createApp(App)
16app.use(unleashPlugin, { unleashClient: client })
17app.mount('#app')

Or, using FlagProvider:

1import { FlagProvider, UnleashClient } from '@unleash/proxy-client-vue'
2
3const config = {
4 url: 'https://UNLEASH-INSTANCE/api/frontend',
5 clientKey: 'FRONTEND_TOKEN',
6 refreshInterval: 15,
7 appName: 'your-app-name',
8}
9
10const client = new UnleashClient(config)
11
12<template>
13 <FlagProvider :unleash-client="client">
14 <App />
15 </FlagProvider>
16</template>

Deferring client start

By default, the Unleash client will start polling for toggles immediately when the FlagProvider component renders. You can delay the polling by:

  • setting the startClient prop to false
  • passing a client instance to the FlagProvider
1<template>
2 <FlagProvider :unleash-client="client" :start-client="false">
3 <App />
4 </FlagProvider>
5</template>

Deferring the client start gives you more fine-grained control over when to start fetching the feature toggle configuration. This could be handy in cases where you need to get some other context data from the server before fetching toggles, for instance.

To start the client, use the client’s start method. The below snippet of pseudocode will defer polling until the end of the asyncProcess function.

1const client = new UnleashClient({
2 /* ... */
3})
4
5onMounted(() => {
6 const asyncProcess = async () => {
7 // do async work ...
8 client.start()
9 }
10 asyncProcess()
11})
12
13<template>
14 <FlagProvider :unleash-client="client" :start-client="false">
15 <App />
16 </FlagProvider>
17</template>

Check flags

To check if a feature is enabled:

1<script setup>
2import { useFlag } from '@unleash/proxy-client-vue'
3
4const enabled = useFlag('travel.landing')
5</script>
6
7<template>
8 <SomeComponent v-if="enabled" />
9 <AnotherComponent v-else />
10</template>

Check variants

To check variants:

1<script setup>
2import { useVariant } from '@unleash/proxy-client-vue'
3
4const variant = useVariant('travel.landing')
5</script>
6
7<template>
8 <SomeComponent v-if="variant.enabled && variant.name === 'SomeComponent'" />
9 <AnotherComponent v-else-if="variant.enabled && variant.name === 'AnotherComponent'" />
10 <DefaultComponent v-else />
11</template>

Defer rendering until flags fetched

useFlagsStatus retrieves the ready state and error events. Follow the following steps in order to delay rendering until the flags have been fetched.

1import { useFlagsStatus } from '@unleash/proxy-client-vue'
2
3const { flagsReady, flagsError } = useFlagsStatus()
4
5<Loading v-if="!flagsReady" />
6<MyComponent v-else error={flagsError} />

Unleash context

Follow the following steps in order to update the unleash context:

1import { useUnleashContext, useFlag } from '@unleash/proxy-client-vue'
2
3const props = defineProps<{
4 userId: string
5}>()
6
7const { userId } = toRefs(props)
8
9const updateContext = useUnleashContext()
10
11onMounted(() => {
12 updateContext({ userId })
13})
14
15watch(userId, () => {
16 async function run() {
17 await updateContext({ userId: userId.value })
18 console.log('new flags loaded for', userId.value)
19 }
20 run()
21})

Developer toolbar

The Developer Toolbar provides runtime flag overrides during development.

1// composables/useUnleash.ts
2import { ref, onMounted } from 'vue'
3import { UnleashClient } from 'unleash-proxy-client'
4import { initUnleashToolbar } from '@unleash/toolbar'
5import '@unleash/toolbar/toolbar.css'
6
7export function useUnleash() {
8 const unleashClient = ref(null)
9 const isReady = ref(false)
10
11 onMounted(async () => {
12 const client = new UnleashClient({ /* your config */ })
13
14 if (import.meta.env.DEV) {
15 unleashClient.value = initUnleashToolbar(client, { themePreset: 'dark' })
16 } else {
17 unleashClient.value = client
18 }
19
20 await unleashClient.value.start()
21 isReady.value = true
22 })
23
24 return { unleashClient, isReady }
25}

Refer to the complete Vue example on GitHub for a full implementation.