> Open Plandalf checkouts from Vue templates with the hosted script or use composables in Vue 3.5 and later.

Source: https://plandalf.com/docs/frameworks/vue

Frameworks

# Vue

Use the hosted script to open checkouts from attributes in your Vue templates. For composables and a gate component, use the [Vue package](#the-plandalf-vue-package).

## Add the script

These are the steps your checkout's **Share** screen shows for Vue. Copy the snippets from there and they already hold your script address and checkout slug.

1.  ### Install the SDK
    
    index.html, before </body>:
    
    ```html
    <script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
    ```
    
2.  ### Add the trigger button
    
    Use the canonical attribute in any component. The SDK's MutationObserver picks up newly mounted elements automatically.
    
    ```vue
    <template>
      <button
        data-plandalf-present="YOUR-CHECKOUT"
        class="btn-primary"
      >
        Buy Now
      </button>
    </template>
    ```
    
3.  ### Make the first open instant (optional)
    
    Paste this under the SDK script tag on the pages that hold the trigger. It fetches the checkout UI (about 1.2 MB) in the background while the page sits idle, so the click only pays for creating the session. Leave it off pages with no checkout: visitors download it whether or not they buy. In a React app, call sdk.preload('checkout') from the ready callback in an effect instead.
    
    ```html
    <script>
      // Optional: fetch the checkout UI while the page is idle, so the first click opens at once.
      window.plandalf = window.plandalf || function () { (window.plandalf.q = window.plandalf.q || []).push(arguments); };
      plandalf('ready', (sdk) => sdk.preload('checkout'));
    </script>
    ```
    

[![The Share screen's Install steps with Vue picked: the script tag, where it goes in index.html, and the trigger button](https://plandalf.com/docs/shots/platforms/share-vue.webp)](https://plandalf.com/docs/shots/platforms/share-vue.webp)

Pick Vue on your checkout's Share screen to get these steps with your own script and checkout slug.

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>My app</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
    <script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
  </body>
</html>
```

Using Nuxt? See the [Nuxt guide](https://plandalf.com/docs/frameworks/nuxt).

## Open a checkout from a button

Add the attribute to any element in a template:

```html
<template>
  <button data-plandalf-present="YOUR-CHECKOUT">Buy Now</button>
</template>
```

### From your own click handler

Use the global `window.plandalf`. Queue the call with the stub, so it works even before the script has loaded:

```html
<script setup lang="ts">
function upgrade() {
  const w = window as any;
  w.plandalf = w.plandalf || function () { (w.plandalf.q = w.plandalf.q || []).push(arguments); };
  w.plandalf("present", "YOUR-CHECKOUT");
}
</script>

<template>
  <button @click="upgrade">Upgrade</button>
</template>
```

See [Open a checkout from code](https://plandalf.com/docs/developers/open-a-checkout).

## Show a checkout on the page

Render an empty element with `data-plandalf-mount`. The script finds it when Vue adds it to the page:

```html
<template>
  <div data-plandalf-mount="YOUR-CHECKOUT" style="min-height: 480px"></div>
</template>
```

## Show a deadline timer

Render an empty element with your deadline timer's slug. The script draws the live countdown inside it. To charge the deadline timer's current price, add `data-plandalf-apply-promo` with the same slug to your buy button.

```html
<div data-plandalf-promo="YOUR-PROMO"></div>
<button data-plandalf-present="YOUR-CHECKOUT" data-plandalf-apply-promo="YOUR-PROMO">Buy Now</button>
```

See [Countdowns](https://plandalf.com/docs/promos/countdowns).

## The @plandalf/vue package

For Vue 3.5 or later, install the package to use Plandalf from your components.

```bash
npm install @plandalf/vue
```

```ts
import { createApp } from 'vue';
import { createPlandalf } from '@plandalf/vue';
import App from './App.vue';

createApp(App)
  .use(createPlandalf({ apiBase: 'https://YOUR-ORG.plandalf.dev' }))
  .mount('#app');
```

Import the composables you need from `@plandalf/vue` and call them in `<script setup>`.

-   Get `present` from `usePresent()`. Call `present('YOUR-CHECKOUT')` when the buyer clicks.
-   Use `usePromo(slug)` to read the current tier and seconds remaining. To show the countdown, take `const { el } = useCountdown(slug)` and add `<div ref="el"></div>` to your template.
-   Use `useGate(name, flow, check)` to check access and offer a checkout to buyers who need it.
-   Use `useUser()` to set the buyer's token and `useReferral()` to read the visitor's referral. Use `usePlandalf()` for the SDK itself.

You can also import `PlandalfGate` from the package. Give `<PlandalfGate>` a `name`, a checkout slug in `flow` and your access check in `:check`. Put the protected content in the default slot. Use `#loading` while Plandalf checks access and `#fallback="{ trigger }"` for an upgrade button that calls `trigger()`.

## Check it's working

1.  ### Run the app
    
    If you used the hosted script, type `typeof window.plandalf` in the browser console. Look for `"function"`.
    
2.  ### Click your button
    
    Plandalf should display your checkout over the page.
    
3.  ### Make a test purchase
    
    Add `localhost` and your staging site in Plandalf under **Settings → Advanced → Test domains**. Checkouts there use Stripe test cards. See [Test mode](https://plandalf.com/docs/get-started/test-mode).
    

## If it isn't working

-   Check that the attribute value matches the slug on the **Share** screen. A bound value (`:data-plandalf-present="slug"`) works too, as long as it's set when the element renders.
-   If your app sends a Content Security Policy, allow `https://*.plandalf.dev` and `https://js.stripe.com` in `script-src`, `connect-src` and `frame-src`.
-   Add `data-debug="true"` to the script tag and the console logs each step the script takes.

See [Troubleshooting](https://plandalf.com/docs/help/troubleshooting).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
