How to import functions from different js file in a Vue+webpack+vue-loader project

前端 未结 4 1132
轮回少年
轮回少年 2021-02-01 01:39

(See end for why this is not a dupe of How do I include a JavaScript file in another JavaScript file?)

Javascipt + Vue + webpack + vue-loader noob... stumbling on the si

相关标签:
4条回答
  • 2021-02-01 01:46

    I like the answer of Anacrust, though, by the fact "console.log" is executed twice, I would like to do a small update for src/mylib.js:

    let test = {
      foo () { return 'foo' },
      bar () { return 'bar' },
      baz () { return 'baz' }
    }
    
    export default test
    

    All other code remains the same...

    0 讨论(0)
  • 2021-02-01 01:56

    After a few hours of messing around I eventually got something that works, partially answered in a similar issue here: How do I include a JavaScript file in another JavaScript file?

    BUT there was an import that was screwing the rest of it up:

    Use require in .vue files

    <script>
      var mylib = require('./mylib');
      export default {
      ....
    

    Exports in mylib

     exports.myfunc = () => {....}
    

    Avoid import

    The actual issue in my case (which I didn't think was relevant!) was that mylib.js was itself using other dependencies. The resulting error seems to have nothing to do with this, and there was no transpiling error from webpack but anyway I had:

    import models from './model/models'
    import axios from 'axios'
    

    This works so long as I'm not using mylib in a .vue component. However as soon as I use mylib there, the error described in this issue arises.

    I changed to:

    let models = require('./model/models');
    let axios = require('axios');
    

    And all works as expected.

    0 讨论(0)
  • 2021-02-01 02:02

    Say I want to import data into a component from src/mylib.js:

    var test = {
      foo () { console.log('foo') },
      bar () { console.log('bar') },
      baz () { console.log('baz') }
    }
    
    export default test
    

    In my .Vue file I simply imported test from src/mylib.js:

    <script> 
      import test from '@/mylib'
    
      console.log(test.foo())
      ...
    </script>
    
    0 讨论(0)
  • 2021-02-01 02:06

    I was trying to organize my vue app code, and came across this question , since I have a lot of logic in my component and can not use other sub-coponents , it makes sense to use many functions in a separate js file and call them in the vue file, so here is my attempt

    1)The Component (.vue file)

    //MyComponent.vue file
    <template>
      <div>
      <div>Hello {{name}}</div>
      <button @click="function_A">Read Name</button>
      <button @click="function_B">Write Name</button>
      <button @click="function_C">Reset</button>
      <div>{{message}}</div>
      </div>
     </template>
    
    
    <script>
    import Mylib from "./Mylib"; // <-- import
    export default {
      name: "MyComponent",
      data() {
        return {
          name: "Bob",
          message: "click on the buttons"
        };
      },
      methods: {
        function_A() {
          Mylib.myfuncA(this); // <---read data
        },
        function_B() {
          Mylib.myfuncB(this); // <---write data
        },
        function_C() {
          Mylib.myfuncC(this); // <---write data
        }
      }
    };
    </script>
    

    2)The External js file

    //Mylib.js
    let exports = {};
    
    // this (vue instance) is passed as that , so we
    // can read and write data from and to it as we please :)
    exports.myfuncA = (that) => {
      that.message =
      "you hit ''myfuncA'' function that is located in Mylib.js  and data.name = " +
        that.name;
    };
    
    exports.myfuncB = (that) => {
      that.message =
      "you hit ''myfuncB'' function that is located in Mylib.js and now I will change the name to Nassim";
      that.name = "Nassim"; // <-- change name to Nassim
    };
    
    exports.myfuncC = (that) => {
      that.message =
      "you hit ''myfuncC'' function that is located in Mylib.js and now I will change the name back to Bob";
      that.name = "Bob"; // <-- change name to Bob
    };
    
    export default exports;
    

    3)see it in action : https://codesandbox.io/s/distracted-pare-vuw7i?file=/src/components/MyComponent.vue


    edit

    after getting more experience with Vue , I found out that you could use mixins too to split your code into different files and make it easier to code and maintain see https://vuejs.org/v2/guide/mixins.html

    0 讨论(0)
提交回复
热议问题