Vue Feature Toggle

3.0.0 · active · verified Tue Apr 21

Vue-Feature-Toggle, currently at stable version 3.0.0, is a specialized Vue.js component library designed to implement feature toggles and variants directly within your Vue templates. It offers a declarative way to conditionally render content based on configurable feature flags. The package integrates with and implements the core logic of the `feature-toggle-api` (v3.4.1), abstracting away the underlying mechanism for managing feature states. This approach separates view logic from feature configuration, allowing non-developers to control feature availability and rollouts, facilitating A/B testing and phased deployments without code changes. The library supports both Vue 2.x and Vue 3.x via its peer dependencies and ships with TypeScript types, enhancing development experience. Unlike imperative, code-centric feature flagging solutions, Vue-Feature-Toggle provides a `<feature>` component for direct use in your markup.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and use the `<Feature>` component in a Vue 3 application. It shows global feature flag configuration using `setFlag` and conditional rendering of content based on these flags, including usage of variants and data attributes.

import { createApp, defineComponent } from 'vue';
import Feature, { setFlag, showLogs } from 'vue-feature-toggle';

// Configure feature flags globally
setFlag('feature1', true);
setFlag('feature2', true);
setFlag('feature3', true);
setFlag('testmode', true);
setFlag('shop', 'de'); // Example for variant-like flag

// Enable logging for debugging
showLogs();

const App = defineComponent({
  components: { Feature },
  template: `
    <div>
      <h1>Vue Feature Toggle Example (v3)</h1>
      <p>Features configured using setFlag in main.ts.</p>

      <Feature name="feature1">
        <p>This is "Feature1" and is currently enabled.</p>
      </Feature>
      
      <Feature name="feature2">
        <p>This is "Feature2" (enabled by default).</p>
      </Feature>

      <Feature name="feature2" variant="new">
        <p>This is "Feature2" with variant "new".</p>
      </Feature>

      <Feature name="feature2" variant="old">
        <p>This "Feature2" with variant "old" is hidden because no 'old' variant is set.</p>
      </Feature>

      <Feature name="feature3" :data="{ message: 'Custom data for feature 3' }" v-slot="{ data }">
        <p>This "Feature3" passes some data: {{ data.message }}</p>
      </Feature>

      <Feature name="testmode">
        <p><strong>Test Mode Active:</strong> Showing debugging information.</p>
      </Feature>

      <Feature name="shop" variant="de">
        <p>Welcome to the German Shop!</p>
      </Feature>

      <Feature name="shop" variant="en">
        <p>Welcome to the English Shop! (Hidden by current config)</p>
      </Feature>
    </div>
  `,
});

const app = createApp(App);
app.mount('#app');

view raw JSON →