首页 > 解决方案 > 从配置文件反应加载变量

问题描述

我现在要使用 React 进行前端开发。

为了确保具有最大的灵活性,我想从特定的 CustomConfigFile 加载一些变量。

如果找不到该文件,则应生成该文件,并且可以在 UI 上加载和编辑该文件。

有人可以告诉我这是否可能并举个例子吗?

标签: reactjsfrontendconfigweb-frontend

解决方案


There are three steps you need to do:

  1. If you want to achieve something like that, you need to store those values either in a database or a file in the server, you can use JSON, XML or whatever format you want.

  2. Your client needs to load this file at runtime, you can use fetch for this.

  3. Now, for the editing part. You are going to need an endpoint you can call from your client, that is able to update those values in the database or in the file. You can use node.js, PHP, ruby or whatever backend language you are comfortable with.

That's the logic you need to implement, I've done it with React + Laravel + MySQL for this app: https://bleext.com/music people can change title, logo, language, the main menu, templates, and so many other things to customize the system.


推荐阅读