Vue reCAPTCHA Component
vue-recaptcha is a Vue component designed for straightforward integration of Google reCAPTCHA v2 (checkbox) and v3 (invisible) into Vue applications. The current stable version is v2.0.3, which primarily supports Vue 3, but also offers Vue 2 compatibility via `@vue/composition-api`. While major version releases (e.g., v1 to v2) have historically had slow cadences, minor bug fixes within a major version are released as needed. A v3.0.0-alpha.6 release is available, indicating future developments. Key differentiators include encapsulating the reCAPTCHA script loading and widget rendering within a declarative Vue component, automatically handling script loading by default since v2.0.0, and shipping with TypeScript definitions for improved developer experience. It simplifies the process of getting a reCAPTCHA token and handling verification events.
Common errors
-
[Vue warn]: Failed to resolve component: VueRecaptcha
cause The VueRecaptcha component was not correctly imported or registered with your Vue application.fixEnsure you are using `import { VueRecaptcha } from 'vue-recaptcha'` and then registering it, e.g., `app.component('VueRecaptcha', VueRecaptcha)` or including it in your component's `components` option. -
TypeError: VueRecaptcha is not a constructor (or is not a function)
cause This error often indicates an incorrect import style, attempting to use a named export as a default export or vice-versa.fixSince v2.0.0, `VueRecaptcha` is a named export. Use `import { VueRecaptcha } from 'vue-recaptcha'`. -
ReferenceError: grecaptcha is not defined
cause The Google reCAPTCHA script (`https://www.google.com/recaptcha/api.js`) has not been loaded or is not available on the global `window` object when the component attempts to render.fixEnsure `:loadRecaptchaScript="true"` is set on the `VueRecaptcha` component, or manually load the script by including `<script src="https://www.google.com/recaptcha/api.js?render=YOUR_SITE_KEY"></script>` in your `index.html` (if `loadRecaptchaScript` is `false`).
Warnings
- breaking VueRecaptcha changed from a default export to a named export.
- breaking For Vue 2 compatibility with vue-recaptcha v2.x, the `@vue/composition-api` package is now a required peer dependency.
- breaking The `loadRecaptchaScript` prop now defaults to `true`, meaning the component will automatically inject the reCAPTCHA script into your page. Previously, it defaulted to `false`.
- gotcha Version 2.0.2 of the package had publishing issues and did not correctly update. It is recommended to skip this version.
Install
-
npm install vue-recaptcha -
yarn add vue-recaptcha -
pnpm add vue-recaptcha
Imports
- VueRecaptcha
import VueRecaptcha from 'vue-recaptcha'
import { VueRecaptcha } from 'vue-recaptcha' - VueRecaptcha
const { VueRecaptcha } = require('vue-recaptcha')import { VueRecaptcha } from 'vue-recaptcha' - VueRecaptchaProps
import type { VueRecaptchaProps } from 'vue-recaptcha'
Quickstart
import { createApp } from 'vue';
import { VueRecaptcha } from 'vue-recaptcha';
const app = createApp({
data() {
return {
sitekey: process.env.VUE_APP_RECAPTCHA_SITE_KEY ?? '',
token: '',
error: ''
};
},
methods: {
onVerify(response) {
this.token = response;
this.error = '';
console.log('reCAPTCHA token:', response);
// Send token to your backend for verification
},
onExpired() {
this.token = '';
console.log('reCAPTCHA token expired.');
},
onError(error) {
this.error = error;
this.token = '';
console.error('reCAPTCHA error:', error);
}
},
template: `
<div>
<h1>reCAPTCHA Example</h1>
<VueRecaptcha
:sitekey="sitekey"
:loadRecaptchaScript="true"
@verify="onVerify"
@expired="onExpired"
@error="onError"
></VueRecaptcha>
<p v-if="token">Token: {{ token }}</p>
<p v-if="error" style="color: red;">Error: {{ error }}</p>
<p v-else-if="!token && sitekey">Waiting for reCAPTCHA verification...</p>
<p v-else>Please set VUE_APP_RECAPTCHA_SITE_KEY environment variable.</p>
</div>
`
});
app.component('VueRecaptcha', VueRecaptcha);
app.mount('#app');