[Vuejs]-Inserting script into VueJS components

0👍

The following component inserts jQuery librery, for some obscure reasons I’m forced to use eval to execute jQuery, I think is related to babel or vue cli.

<template>
  <div>
    <button @click="validate">Click me</button>
    <div id="div1">Check Load</div>
  </div>
</template>

<script>
  export default {
    data: () => ({
    }),

    mounted() {
      const myScript = document.createElement('script');
      myScript.setAttribute('src', 'https://code.jquery.com/jquery-3.3.1.min.js');

      const div1 = document.getElementById("div1");
      div1.appendChild(myScript);
    },
    methods: {
      validate () {
        eval(`$('#div1').html('jQuery Loaded')`);
      },
    }
  }
</script>
👤miorey

Leave a comment