How to Import a Script into .Vue File

How to include local script files in Vue

Import like this

<script>
import * as myKey from '.src/..';

export default {

}
</script>

How can I include 3rd party JavaScript files in vue.js?

There seems to be no real solution, which means no solution without modifying the original 3rd party script.
The reason is that the 3rd party script consists of an "Immediately Invoked Function Expression" (IIFE):

(function (win) {
win.MyUtil = {
"func1": function func1() { ... },
"func2": function func1() { ... }
}
}(window));

So I had to modify the third party script, which is what I wanted to avoid. Thanks to Rishinder (VPaul) who pointed to the right direction.
Now it is a "Module" that exports an object:

var MyUtil
export default MyUtil = {
func1: function() { ... },
func2: function() { ... }
}

In the Vue.js single file component file (*.vue) it can be imported like this (if it is in the same folder as the *.vue file):

<script>
import MyUtil from "./my_util.js"
// code using MyUtil goes here
</script>


Related Topics



Leave a reply



Submit