7

I have a VueJS project that I started it with vue-cli webpack. I have a javascript 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;
}());

Project structure

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

Thanks.

2
  • Since you are using vue-cli, you are probably also using webpack. Start by taking a look at Single File Components and Webpack: Getting Started. Commented Jan 11, 2018 at 21:18
  • If you have webpack you can use import to lnclude external JavaScript code, but your library should also export its functions / classes. You could also just load the javascript file with a <script> tag in your HTML Commented Jan 11, 2018 at 21:23

1 Answer 1

11

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Good, but.....is there no possibility to use libraries.js in a module without exporting its properties?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.