VueJs: importing custom js libraries

thanks for reading this. I have a Vuejs project that I started it with vue-cli, webpack. I have a JS library with functions that I need in all components. I wonder where I need to place this folder and how to call the functions from JSLibrary1 in Component1.vue:

-myJSLibrary JSLibrary1.js JSLibrary2.js

JSLibrary1.js

var A = A || (function() {
    class B {
        function C(){
            return “hello”;
        }   
    }       
    var obj = new B();
    return obj;
}());



VueProject
   build
   config
   src
    assets
    components
        Component1.vue
    App.vue
    main.js
   static

Thanks.

1 answer

  • answered 2018-01-11 21:08 tribe

    libraries.js

    export const A = () => {
      // your code
    }
    
    export const B = () => {
      // your code
    }
    

    Component1.vue

    <script>
    import { A, B } from '~/path/to/libraries.js
    export default {
      data () {
        return {}
      },
      mounted () {
        // execute A when components is rendered
        A()
      }    
    </script>