I also ran into this problem and it took me 2 days to solve this problem.
So, I will provide a general answer for integrating recaptcha with vue.js from scratch step by step, to be an easy guide for people who will be in the same situation in the future (I assume vue-cli is used here).
Note: I am using an invisible recaptcha here, but the process is pretty similar to normal
Step 1:
add recaptcha javascript api to your index.html
index.html
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
Step 2:
create a component called Recaptcha or whatever you want to call it (creating a component will make it easier to read the code, make it easier, and make it easier to add recaptcha to more than one page if you need to)
Recaptcha.vue
<template> <div id="g-recaptcha" class="g-recaptcha" :data-sitekey="sitekey"> </div> </template> <script> export default { data () { return { sitekey: '6LfAEj0UAAAAAFTGLqGozrRD8ayOy*********', widgetId: 0 } }, methods: { execute () { window.grecaptcha.execute(this.widgetId) }, reset () { window.grecaptcha.reset(this.widgetId) }, render () { if (window.grecaptcha) { this.widgetId = window.grecaptcha.render('g-recaptcha', { sitekey: this.sitekey, size: 'invisible', </script>
Step 3:
Import the recaptcha component and add it to your page (parent component).
page.vue
<template> <div> <h1>Parent component (your page)</h1> <button @click="executeRecaptcha">execute recaptcha</button> <recaptcha ref="recaptcha" @verify="submit"></recaptcha> </div> </template> <script> import Recaptcha from 'recaptcha' export default { components: { Recaptcha }, methods: { </script>
Abdelaziz mokhnache
source share