jeudi 8 novembre 2018

How to add external script to vue component?

I need your help very much... I have a Vue component and endpoint which gives me a script with a small menu and that script includes some actions. So, after my script is loaded its actions don't work on a page and I don't know why. Below the example code:

<template>
  <div class="menu"></div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  mounted () {
    // here I get a script which contains menu bar with actions
    // css and styles 
    this.$http.get("https://endpointwithscript.com/menu").then(response => {
      if (response.status === 200) {
        //in that case script doesn't work. Eg click
        var div = document.getElementById("menu")
        div.innerHTML += response.body;
        document.appendChild(div);
      }
    })
  }
}
</script>

If I insert my downloaded script that way :

mounted () {
    this.$http.get("https://endpointwithscript.com/menu").then(response => {
      if (response.status === 200) {
        document.write(response.body);
      }
    })
  }

then script works but another html elements are overridden by that script and not displayed. How to download script, insert it on a page and keep all functionality ? Thank you!




Aucun commentaire:

Enregistrer un commentaire