1、微信小程序使用ext.json

在项目根目录定义ext.json文件

ext.json
{
  "extEnable": true,
  "ext": {
    "shopId": 20
  }
}

然后

if (wx.getExtConfig) {
  wx.getExtConfig({
    success(res) {
      console.log(res.extConfig)//{shopId:20}
    }
  })
}

官方文档:

https://developers.weixin.qq.com/miniprogram/dev/api/wx.getExtConfig.html

2、Taro使用ext.json

由于Taro每次构建都会删掉dist文件,故直接把ext.json文件放到dist根目录下(可行)不是很友好;Taro官网给出一个文件 copy 配置:

config/index.js文件:

 copy: {
    patterns: [
      { from: 'ext.json', to: 'dist/ext.json' } // 指定需要 copy 的文件
    ],
    options: {
    }
  },

然后可以愉快的使用了

if (wx.getExtConfig) {
  wx.getExtConfig({
    success(res) {
      console.log(res.extConfig)//{shopId:20}
    }
  })
}

哦!记得要重新构建哦!

官网地址:

https://nervjs.github.io/taro/docs/config-detail.html

 

 

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐