How can I load a JavaScript script dynamically in a Vue.js app?
Here's a naive solution:
<script async v-bind:src="srcUrl"></script>
<!--<script async src="https://cse.google.com/cse.js?cx=007968012720720263530:10z7awj2l37"></script>-->
But the first line doesn't load the script (it does not event add the script
element to HTML).
The second line works. The second line is identical, just the app variable is replaced with plain text (srcUrl => https://cse.google.com/cse.js?cx=007968012720720263530:10z7awj2l37
).
Where's my mistake?
The full demo for reference (it is supposed to load a Google Custom Search Engine on a blank page):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
</head>
<body>
<div id="load-script">
<div>{{ srcUrl }}</div>
<div class="gcse-searchbox"></div>
<div class="gcse-searchresults"></div>
<script async v-bind:src="srcUrl"></script>
<!--<script async src="https://cse.google.com/cse.js?cx=007968012720720263530:10z7awj2l37"></script>-->
</div>
<script>
new Vue({
el: '#load-script',
data: {
srcUrl: "https://cse.google.com/cse.js?cx=007968012720720263530:10z7awj2l37"
}
});
</script>
</body>
</html>
I found related questions but they don't seem to describe this situation exactly: