简体   繁体   English

如何在 VueCLI 3 中全局获取`webpack.config.js` 或 `vue.config.js` 文件数据

[英]How to get `webpack.config.js` or `vue.config.js` file data globally in VueCLI 3

I'm working on a project which develop by VueCLI 3.0 .我正在开发一个由 VueCLI 3.0开发的项目。 I've Created a config file in the root and named as vue.config.js .我在根目录中创建了一个配置文件并命名为vue.config.js I've defined my custom configuration.我已经定义了我的自定义配置。 but every time I've to load this file in every component file to use it.但是每次我必须在每个component文件中加载这个文件才能使用它。 Is there any way or option to get globally in every js file and component .有什么方法或选项可以在每个js文件和component全局获取。

I'm newbie in VueCli.我是 VueCli 的新手。

Here, is my vue.config.js file :-这是我的vue.config.js文件:-

// vue.config.js
module.exports = {
      api : {
          server: 'http//:192.168.0.1'
      }
  }

In my App.vue - I want to access this way.在我的App.vue - 我想以这种方式访问​​。

<script>
export default{
  mounted(){
    console.log(api.server)
  }
}
</script>

Please help me out and give suggestions请帮助我并提出建议

It not recommend to use data in vue.config.js directly.不建议直接使用vue.config.js数据。 If you have some variables based on environment, use Modes and Environment Variables .如果您有一些基于环境的变量,请使用Modes 和 Environment Variables

Its not recommended method even if you need to do that just try this.即使您需要这样做,它也不推荐使用此方法

import vueconfig from "./../vue.config.js";
new Vue({
    data:{
        config
    }
})

one thing you remember that we can't define custom data in vue.config.js see this , If you want custom config rename the file.你记得我们不能在vue.config.js定义自定义数据的一vue.config.js看到这个,如果你想要自定义配置重命名文件。

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

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