简体   繁体   中英

How to reuse local file.js in several vue projects?

My file.js has some functions that I would like to reuse in several vue projects, specifically within App.vue

the file structure is:

-- projec1
---- src
------ App.vue

-- project2
---- src
------ App.vue

-- myfile.js

I can't import it directly like that

import * as alias from '../../myfile.js'

because the file path is not found during the production build

ERROR Failed to compile with 1 error3: 21: 38 PM
This relative module was not found: ../../myfile.js

Is there a simple way to do this? Something in package.json like

{
    myLocalDependency: path/to/myFile.js
}

and in App.vue to be able to do

<script>
import myLocalDependency from '...'

export default {
     mounted: {
         myLocalDependency.myPreciousFunction()
     }
}
</script>

if your file.js is not very complicated you can use Adding Instance Properties to your Vue.js application. for example in your main.js file you should do something like this:

function converter(){}
Vue.prototype.$converter = converter();

And then you can use it anywhere in your vue project just like this.$converter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM