[英]How to design a vue-cli app that be configured after build?
I have a working vue-cli app, that I can build without a problem. 我有一个工作的vue-cli应用程序,我可以毫无问题地构建。 Now, I want to package a single build that will be deployed on several servers.
现在,我想打包一个将部署在多个服务器上的构建。 My problem is that, depending on the server, I'll have to tweak some simple variables (server port, API token, etc.).
我的问题是,根据服务器的不同,我将不得不调整一些简单的变量(服务器端口,API令牌等)。
Clearly, it's not a suitable solution to run several builds (based on .env files, or whatever) because of the context. 显然,由于上下文,它不适合运行多个构建(基于.env文件或其他)。 I often get settings information on site and have to configure them quickly.
我经常在网站上获取设置信息,并且必须快速配置它们。
Before working with Webpack and all its underlying compilation process, I had a classic js file embedding the settings and I would like to produce something similar. 在使用Webpack及其所有底层编译过程之前,我有一个嵌入设置的经典js文件,我想生成类似的东西。 For what I know, files created on the
public
folder are not reachable from vue components (with import directive) and once minified, it's not a solution to tweak settings. 据我所知,在
public
文件夹上创建的文件无法从vue组件(使用import指令)访问,一旦缩小,它不是调整设置的解决方案。
Is it possible to tell vue-cli3 or webpack to keep a specific file or folder "as is"? 是否可以告诉vue-cli3或webpack“按原样”保存特定文件或文件夹? Or maybe that there is a way cleaner solution?
或者也许有一种更清洁的解决方案?
Well, just in case someone, one day, will need to do something similar, here is my solution: 好吧,万一有人,有一天,需要做类似的事情,这是我的解决方案:
settings.json
settings.json
In the App.vue component, I specify a mounted()
method that will be automatically called at component launch (see VueJs components lifecycle if needed). 在App.vue组件中,我指定了一个在组件启动时自动调用的
mounted()
方法(如果需要,请参阅VueJs组件生命周期)。 This methods: 这种方法:
Asynchronously query the static settings.json
file to get the current file (with Axios in my case) 异步查询静态
settings.json
文件以获取当前文件(在我的情况下使用Axios)
Store the values in a dedicated store section (see VueX or any simpler store structure). 将值存储在专用存储区中(请参阅VueX或任何更简单的存储结构)。 In my case this looks something like:
在我的情况下,这看起来像:
Store.ts Store.ts
let store = {
...
// Will receive our config.json file content
settings: {},
};
export default store;
App.vue App.vue
mounted() {
Axios.get('./settings.json')
.then((response) => {
Store.Settings = response.data;
})
}
This may not be perfect, but there is no need of an external technology, settings reachable from every component just like your states are in the store. 这可能并不完美,但不需要外部技术,可以从每个组件到达的设置,就像您的状态在商店中一样。
Let me know if that sounds weird or there is a better solution. 如果这听起来很奇怪或者有更好的解决方案,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.