After the uniapp project packages H5, it is hoped that the fixed configuration (interface address, system name, etc.) can be modified

1. Create config.js in the static static directory, as shown in the figure

 config.js

const globalData = {
    localBaseUrl: 'https://www.xxxx.com/service/api', // 现测试地址
    corpId:'ding562e6256565rw56r323t3ff3fgghhh2351' //公司的钉钉id
}

2. Set "template" under h5 of manifest.json: "template.h5.html"

 

3. Create a new template.h5.html file in the root directory of the project, and import the configuration file config.js into this file

 template.h5.html file content, import configuration file config.js

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <script>
        var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
        document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')
    </script>
    <script src="./static/configjs/config.js"></script>
    <link rel="stylesheet" href="<%= BASE_URL %>static/index.<%= VUE_APP_INDEX_CSS_HASH %>.css" />
</head>
<body>
    <noscript>
        <strong>Please enable JavaScript to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
</body>
</html>

4. Re-run the uniapp project, be sure to restart it after configuration

5. How to use

Just use it where you need it

const serviceIP = globalData.localBaseUrl;//接口配置

The configuration method of vue can be viewed in this article written before

After the vue project is packaged, it is hoped that the configuration (interface address, system name, etc.) can be modified and released. Afterwards, there may be interface domain name changes, project name changes, etc., requiring developers to re-modify the code and re-release, which is time-consuming and laborious. It is hoped that these configurations can be stored in one file. If it needs to be replaced, the operation and maintenance can directly open the configuration file, and the changes will take effect immediately after saving. ...... https://blog.csdn.net/QQ_Empire/article/details/126248877?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/QQ_Empire/article/details/128142746