gpt4 book ai didi

javascript - vue动态加载recaptcha脚本

转载 作者:行者123 更新时间:2023-11-28 17:44:02 27 4
gpt4 key购买 nike

我想动态加载诸如recaptcha之类的脚本,因为我只想在Register.Vue/login.Vue组件内加载

<script src="https://www.google.com/recaptcha/api.js?onload=vueRecaptchaApiLoaded&render=explicit" async defer>
</script>

如果我将脚本标记放入 Vue 文件中,则会收到此错误:

  - Templates should only be responsible for mapping the state to the UI. Avoid
placing tags with side-effects in your templates, such as <script>, as they will
not be parsed.

我该如何解决这个问题?

最佳答案

在您的登录 Vue 页面中,添加:

methods: {

// create
createRecaptcha () {
let script = document.createElement('script')
script.setAttribute('async', '')
script.setAttribute('defer', '')
script.id = 'recaptchaScript'
script.src = 'https://www.google.com/recaptcha/api.js?onload=vueRecaptchaApiLoaded&render=explicit'
script.onload = function () {
document.getElementsByTagName('head')[0].appendChild(script)
}
},

// remove
removeRecaptcha () {
document.getElementById('recaptchaScript').remove()
}

},

// your code ...

mounted () {
this.createRecaptcha()
},

beforeDestroy () {
this.removeRecaptcha()
}

关于javascript - vue动态加载recaptcha脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47216197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com