Utilizing Multi External CDN JavaScript File with Vue CLI Component: A Comprehensive Guide

I've been trying different methods to include external JS files in a Vue Component, such as using mounted() and created(), but unfortunately, none of them have worked for me so far. I'm not sure where I'm going wrong. Any assistance would be greatly appreciated :)

Below is the code snippet:

<template>
  <div id="sketchy"></div>
</template>

<script>

export default {
  data(){
    return {
    }
  }, 
  mounted() {
      if (document.getElementById('sketchy')) return; // was already loaded
      var scriptTag = document.createElement("script");
      scriptTag.src = "https://bootswatch.com/_vendor/jquery/dist/jquery.min.js";
      scriptTag.id = "sketchy";
      document.getElementsByTagName('head')[0].appendChild(scriptTag);
  },
   mounted() {
      if (document.getElementById('sketchy')) return; // was already loaded
      var scriptTag = document.createElement("script");
      scriptTag.src = "https://bootswatch.com/_vendor/popper.js/dist/umd/popper.min.js";
      scriptTag.id = "sketchy";
      document.getElementsByTagName('head')[0].appendChild(scriptTag);
  },
  mounted() {
      if (document.getElementById('sketchy')) return; // was already loaded
      var scriptTag = document.createElement("script");
      scriptTag.src = "https://bootswatch.com/_vendor/bootstrap/dist/js/bootstrap.min.js";
      scriptTag.id = "sketchy";
      document.getElementsByTagName('head')[0].appendChild(scriptTag);
  },
}
</script>


<style scoped>

</style>

Answer №1

This method was attempted by me. I imported the 3 script files within the head section.

mounted:function(){
//   if (document.getElementById('sketchy')) {
//       console.log("loadeddd");
//         return;
//   } // was already loaded
  var scriptTag = document.createElement("script");
//   scriptTag.src = "https://bootswatch.com/_vendor/jquery/dist/jquery.min.js";
  scriptTag.setAttribute('src','https://bootswatch.com/_vendor/jquery/dist/jquery.min.js?onload=onLoad')
//   scriptTag.id = "sketchy";
  //scriptTag.setAttribute('id','sketchy');
  scriptTag.async=true;
  scriptTag.defer=true;
  document.getElementsByTagName('head')[0].appendChild(scriptTag);
  console.log("scripttag1 createddd");

  var scriptTag1 = document.createElement("script");
  scriptTag1.setAttribute('src','https://bootswatch.com/_vendor/popper.js/dist/umd/popper.min.js?onload=onLoad');
  scriptTag1.async=true;
  scriptTag1.defer=true;
  document.getElementsByTagName('head')[0].appendChild(scriptTag1);
  console.log("scriptag2 createdd");


  var scriptTag2 = document.createElement("script");
  scriptTag2.setAttribute('src','https://bootswatch.com/_vendor/bootstrap/dist/js/bootstrap.min.js?onload=onLoad');
  scriptTag2.async=true;
  scriptTag2.defer=true;
  document.getElementsByTagName('head')[0].appendChild(scriptTag2);
  console.log("scriptag3 createdd");

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In a REST api, what is the appropriate response for a property that is missing a value?

Is it better for a property with no value assigned to be returned as null, or should the REST API skip such properties entirely? Let's consider a user object example with first_name and last_name. (In the below example, last_name is not necessarily a ...

How to extract the date value from an HTML date tag without using a datepicker

Utilizing Intel's app framework means there is no .datepicker method available. The following code snippet generates the date widget within my app: <label for="startdate">Start Date:</label> <input type="date" name="startdate" id="star ...

Retrieve data from the redux store within the components nested under redux-simple-router

I am currently working on finding a way to access the redux store within a route in order to dispatch actions directly from that location. Below is an example of how my main Component is structured: class App extends Component { render() { return ( ...

Attempting to show different fields depending on the chosen option

Having an issue with the signup process for my team and competition setup. I want to implement a feature where, if the user selects 'Competition', the promo code field will be displayed. However, this field should only appear when 'Competiti ...

Having trouble bypassing custom points on the reactive gauge speedometer

My current project involves utilizing the npm package react-d3-speedometer to create a custom points-based gauge. The issue I am facing is that while the package works properly with values from 0 to 1000 when passed to the customSegmentValues property, it ...

Is SSG the best approach for deploying a Nuxt.js dashboard?

We have plans to create an internal dashboard utilizing Nuxt.js. Since this dashboard will be used internally, there is no requirement for a universal mode. Typically, most dashboards are designed as SPAs (Single Page Applications). However, SPAs still ne ...

Learn how to efficiently transfer row data or an array object to a component within Angular by utilizing the MatDialog feature

My goal is to create a functionality where clicking on a button within a specific row opens up a matDialog box displaying all the contents of that row. Below is the HTML snippet: <tr *ngFor="let u of users"> <td data-label="ID& ...

Troubleshooting 'Warning: Prop `id` did not match` in react-select

Having an issue with a web app built using ReactJs and NextJs. I implemented the react-select component in a functional component, but now I'm getting this warning in the console: Warning: Prop id did not match. Server: "react-select-7 ...

Utilizing twig variables with Vue.js within Symfony 4

I have integrated a component called "vue-cookie-law" into my Symfony application. In the default Twig template, I call it like this: <div id="CookieLaw" data-locale="{{ app.request.locale }}"></div> In my Vue template, I have the following c ...

Steps for creating a functional counter in AngularJS

I am attempting to create a counter in AngularJS that will be used for some other purpose once it is working with a variable. However, I am facing an issue where the variable is not updating as expected. Since this will eventually become a more complex com ...

Is it possible to dynamically insert a ng-mouseover in AngularJS using Javascript?

It seems like there is an issue with this code: var run_div = document.createElement('div'); run_div.className = 'whatever'; run_div.textContent = 'whatever'; run_div.setAttribute('ng-mouseover', 'console.log(&b ...

Library package for Selenium web driver

I'm new to using Selenium web driver As I started writing my code, an error occurred in the first line. The package is showing as not accessible." Any assistance would be greatly appreciated. https://i.sstatic.net/vYUmS.png ...

Using a template literal as a prop is causing rendering issues

I have a functional component const CustomParagraph = forwardRef((ref: any) => { return ( <div> <p dangerouslySetInnerHTML={{ __html: props.text }}></p> </div> ); }); Whenever I use this component, I am unable ...

Position object in the middle using jQuery and CSS

Trying to center an absolutely positioned object horizontally using CSS and jQuery is proving to be a challenge. The use of jQuery is necessary due to the varying widths of the objects. Hover over the icons in my jsFiddle to see the issue. Check out the j ...

Extracting information from a designated website using Python, including pages with search functionality and javascript features

Visit the website where you will find a search input box in html. Input a company name into the search box, select the first suggestion from the drop-down menu (like "Anglo American plc"), navigate to the URL containing information about that specific com ...

Is it possible to utilize the returned value of a function within an if statement?

Is there a way to return the result of a function without needing to declare a variable? Can you return the result of a function in a single line? How can you return the result of a function inside an if statement? Is it possible to use a function's ...

Using Typescript with Material UI Select

I have implemented a dropdown menu using Material UI select labeled "Search By." When the menu is clicked, it displays a list of options. I aim to store the selected option and update the label "Search By" with the chosen option. export default function U ...

Vue.JS component containing DOM elements outside of the designated $el scope

Transitioning from a custom front-end framework to Vue is a new adventure for me. Our website is gradually integrating Vue, and as we refactor old components, I've encountered an issue. My question is: Can a component create DOM elements outside of i ...

Creating dynamic headers in Axios within an ExpressJS application

I have a specific requirement that calls for setting a custom Request-Id header for each axios request. The value of this header is generated dynamically by a separate express middleware. While I could manually set the header like this: axios.get(url, he ...

Sending JSON data from the primary window to the secondary window in Electron - A step-by-step guide

I am currently working with a set of 3 files. index.html: ... <a href="#" onclick="fetch(function(data) {console.log(data); subWindow(data)})">subWindow</a> ... The fetch() function retrieves a callback in JSON format. subWindows.js: let m ...