简体   繁体   English

如何设计构建后配置的vue-cli应用程序?

[英]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: 好吧,万一有人,有一天,需要做类似的事情,这是我的解决方案:

  1. I create un config file in the public dir, like settings.json 我在public dir中创建un配置文件,比如settings.json
  2. 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: 这种方法:

    1. Asynchronously query the static settings.json file to get the current file (with Axios in my case) 异步查询静态settings.json文件以获取当前文件(在我的情况下使用Axios)

    2. Store the values in a dedicated store section (see VueX or any simpler store structure). 将值存储在专用存储区中(请参阅VueX或任何更简单的存储结构)。 In my case this looks something like: 在我的情况下,这看起来像:

Long story short: 长话短说:

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.

相关问题 vue-cli 构建失败并出现编译错误 - vue-cli build fails with compile errors 如何使用 typescript/vue-router/vuex/ 但不使用 vue-cli 创建 vue.js3 应用程序 - How to create vue.js3 app using typescript/vue-router/vuex/ but without vue-cli 如何使用 Webpack (vue-cli) 从生产构建中排除 CommonJS 库 - How to exclude CommonJS libs from production build with Webpack (vue-cli) 使用 vue-cli 创建新项目后没有属性“组件”错误 - No property 'component' error after creating new project with vue-cli Vue-CLI、TypeScript 和 monorepo:如何在 /src 之外声明类型? - Vue-CLI, TypeScript and monorepo: how to declare types outside of /src? 同步VSCode linting和vue-cli linting - Synchronising VSCode linting with vue-cli linting 使用vue-cli serve时如何禁用棉绒? - How do you disable linting when using vue-cli serve? 如何在带有 typescript 的 vue-cli 中使用自定义服务工作者事件 - How can I use a custom service worker events in vue-cli with typescript 将现有项目重构为新的 vue-cli 创建的项目时,如何保留 Git 提交历史记录? - How to preserve Git commit history when refactoring an existing project to a fresh vue-cli created one? 如何在vue-cli 3项目中禁用ts-loader(tslint)检查? - How to disable ts-loader (tslint) checking in vue-cli 3 project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM