Home Guides Vue

How to add a quiz to a Vue app

Embedding a LeadForms quiz in a Vue application
In the quiz editor, open “Publish” → “On website” and set up how it’s shown
Choose where the quiz appears (a window over the site, a side panel or a block in the page) and how it opens, for example with the LeadForms button, auto-open or your website’s own links and buttons. Then click the “Publish” button at the bottom of the “Publish” tab: without it, your settings don’t reach your website.
“Publish” → “On website” tab in LeadForms: the “Where the quiz appears” and “How it opens” steps and the “Publish” button
Copy the LeadForms code
Open the “Site code” block and copy the code. It’s one code for the whole app, and it loads all your quizzes and widgets: you don’t need to add it to every component.
“Publish” → “On website” tab in LeadForms: the “Site code” block with the code to paste
Paste the script into index.html
Open index.html in the project root (in Vite it sits next to package.json, in Vue CLI it’s in the public folder) and paste the copied code right before the closing </body> tag.
Why here and not in a component: Vue doesn’t run <script> tags inside templates, so code pasted there simply won’t run.
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>My website</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
    <script async src="https://script.leadforms.org/v1.js" data-key="YOUR_KEY"></script>
  </body>
</html>
Open the quiz from your own button
The simplest way is to add the data-leadforms attribute with the quiz ID to your button. No click handler needed:
<template>
  <button type="button" data-leadforms="8233">
    Take the quiz
  </button>
</template>
If you need to open the quiz from your own handler, call Leadforms.open with the quiz ID:
<template>
  <button type="button" v-on:click="openQuiz">
    Take the quiz
  </button>
</template>

<script setup>
function openQuiz() {
  if (window.Leadforms) {
    window.Leadforms.open(8233)
  }
}
</script>
Replace 8233 with your quiz ID: it’s the number in the direct link https://quiz.leadforms.org/ID.
Using a link
The quiz also opens from a regular link with the address #leadforms-ID:
<a href="#leadforms-8233">Take the quiz</a>
Important: it has to be a plain <a>, not <router-link>. Vue Router would intercept the click and look for a route instead of passing the hash to the widget.
If you want the quiz as a block inside the page
In “Publish”, choose “Block in the page” and add a container with the quiz ID to the component template:
<template>
  <div data-leadforms-block="8233"></div>
</template>
The block is picked up on its own, even if the component mounts after the page has loaded. If the block still doesn’t show up, call window.Leadforms.refresh() in onMounted.
What to keep in mind in an SPA
• The LeadForms code loads once, when the app opens. It doesn’t reload when you switch routes, and that’s expected.
• Blocks and buttons that appear when you open a route are picked up on their own. If something doesn’t show up, call window.Leadforms.refresh().
• A button with data-leadforms and a Leadforms.open call work on any route with no extra steps.
Having trouble with the setup?
If something doesn't work, and we'll sort it out with you.

Still have questions? Write to us

Name *
* Required field
Email *
* Required field
Message
Write to us!
We reply within 30 seconds