简体   繁体   English

如何在多个vue项目中复用本地file.js?

[英]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我的 file.js 有一些我想在几个 vue 项目中重用的功能,特别是在 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 package.json 之类的东西

{
    myLocalDependency: path/to/myFile.js
}

and in App.vue to be able to do并且在 App.vue 中能够做到

<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.如果您的 file.js 不是很复杂,您可以使用将实例属性添加到您的 Vue.js 应用程序。 for example in your main.js file you should do something like this:例如在您的main.js文件中,您应该执行以下操作:

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

And then you can use it anywhere in your vue project just like this.$converter然后你就可以在你的 vue 项目的任何地方使用它,就像this.$converter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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