我想动态加载脚本,比如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.我如何解决这个问题呢?
发布于 2017-11-10 05:35:40
在登录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()
}https://stackoverflow.com/questions/47216197
复制相似问题