I have to load an external javascript file in order to have access to a certain object.
Normally the code is very simple:
<script src='https://example.com/example.js'></script>
<script>
var instance = new ExampleObj.start({ container: 'element-id' });
</script>
<div id='element-id'></div>
How do I accomplish this in a .vue
file?
This did not work:
export default {
...
mounted() {
const script = document.createElement('script')
script.setAttribute('src', 'https://example.com/example.js')
const start = document.createElement('script')
start.text = `var instance = new ExampleObj.start({ container: 'element-id' });`
document.body.appendChild(script)
document.body.appendChild(start)
}
...
}
The ^ above examples gives error: ExampleObj not defined
, however if I try to access ExampleObj
it shows up on the developer console with the start
method working.