Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing javascript file for use within vue component

I am working on a project that requires using a js plugin. Now that we're using vue and we have a component to handle the plugin based logic, I need to import the js plugin file within the vue component in order to initialize the plugin.

Previously, this was handled within the markup as follows:

<script src="//api.myplugincom/widget/mykey.js "></script> 

This is what I tried, but I am getting a compile time error:

MyComponent.vue

import Vue from 'vue'; import * from  '//api.myplugincom/widget/mykey.js';  export default {     data: { 

My question is, what is the proper way to import this javascript file so I can use it within my vue component? ...

like image 513
AnchovyLegend Avatar asked Jan 24 '18 16:01

AnchovyLegend


People also ask

How do I import a JavaScript file into Vue component?

There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.

How do I use an external js file in Vue component?

Add a script tag inside your Vue component template. This is the easy fix I have found. Just add the script into the component template. But don't forget to add, type="application/javascript" to the script tag otherwise it will show an error during the build. However, if you have added document.

Can I use JavaScript in Vue?

In Vue. js, components can be written in HTML, CSS, and JavaScript without dividing them into separate files.


2 Answers

Include an external JavaScript file

Try including your (external) JavaScript into the mounted hook of your Vue component.

<script> export default {   mounted() {     const plugin = document.createElement("script");     plugin.setAttribute(       "src",       "//api.myplugincom/widget/mykey.js"     );     plugin.async = true;     document.head.appendChild(plugin);   } }; </script> 

Reference: How to include a tag on a Vue component

Import a local JavaScript file

In the case that you would like to import a local JavaScript in your Vue component, you can import it this way:

MyComponent.vue

<script> import * as mykey from '../assets/js/mykey.js'  export default {   data() {     return {       message: `Hello ${mykey.MY_CONST}!` // Hello Vue.js!     }   } } </script> 

Suppose your project structure looks like:

src - assets     - js       - mykey.js - components     MyComponent.vue 

And you can export variables or functions in mykey.js:

export let myVariable = {}; export const MY_CONST = 'Vue.js'; export function myFoo(a, b) {     return a + b; } 

Note: checked with Vue.js version 2.6.10

like image 82
Yuci Avatar answered Sep 29 '22 08:09

Yuci


try to download this script
import * from '{path}/mykey.js'.
or import script
<script src="//api.myplugincom/widget/mykey.js"></script> in <head>, use global variable in your component.

like image 25
JJJ Avatar answered Sep 29 '22 07:09

JJJ