Answer the question
In order to leave comments, you need to log in
How to force a script tag to execute from an API in VUE?
I have a component that gets content from an API. The content may include layout and a script with a map from the Yandex constructor.
For example:
<script type="text/javascript" charset="utf-8" src="//api-maps.yandex.ru/services/constructor/1.0/js/?sid=cmW8oHaPfpTIUddKnh6jsAZNzv8HxmaT&width=100%&height=450"></script>
<template lang="pug">
div(v-html="сontent")
</template>
Answer the question
In order to leave comments, you need to log in
The essence of the problem, as the documentation suggests :
HTML5 specifies that a <script>
tag inserted with innerHTML should not execute .
script
manually. More precisely, we will replace the one that is with a new one with the same src. div(v-html="сontent" ref="content")
watch: {
content() {
this.$nextTick(() => {
const oldScript = this.$refs.content.querySelector('script');
const newScript = document.createElement('script');
newScript.src = oldScript.src;
oldScript.parentNode.replaceChild(newScript, oldScript);
});
},
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question