找回密码
 立即注册
首页 业界区 业界 使用TypeScript开发微信小程序(云开发)-入门篇 ...

使用TypeScript开发微信小程序(云开发)-入门篇

阎逼 前天 22:02
配置小程序云开发 TypeScript 环境

1. 检查本地 nodejs 环境

1.png

2. 安装 TypeScript npm install typescript --save-dev

2.png

3. 初始化/配置 TypeScript

3.1 初始化 ./node_modules/.bin/tsc --init

3.png

3.2 修改tsconfig.json配置
  1. {
  2.   "compilerOptions": {
  3.     "target": "es2016",
  4.     "module": "commonjs",
  5.     "typeRoots": ["./typings"],
  6.     "esModuleInterop": true,
  7.     "strict": true,
  8.     "skipLibCheck": true
  9.   },
  10.   "include": ["./miniprogram/**/*.ts", "./cloudfunctions/**/*.ts"],
  11.   "exclude": ["node_modules"]
  12. }
复制代码
3.3 添加 TypeScript .d.ts 文件


  • 在项目根目录新建文件夹typings(与 tsconfig.json>compilerOptions>typeRoots 配置一致)
  • 复制 https://github.com/wechat-miniprogram/api-typings/tree/master/types 目录下的所有文件到 typings 文件夹
  • 如果无法访问,也可从这里下载: typings.zip
4.png

4. 配置编译命令

4.1 在package.json添加scripts命令
  1. "tsc": "node ./node_modules/typescript/lib/tsc.js"<br>
复制代码
5.png

4.2 修改project.config.json,添加自定义处理命令
  1. {
  2.   "scripts": {
  3.     "beforeCompile": "npm run tsc",
  4.     "beforePreview": "npm run tsc",
  5.     "beforeUpload": "npm run tsc"
  6.   }
  7. }<br>
复制代码
6.png

4.3 在“微信开发者工具”中启用自定义处理命令

7.png

示例

1. 调用微信云函数获取微信步数
  1. // 小程序端,获取微信步数相关数据
  2. async getWxRunData(): Promise<WechatMiniprogram.GetWeRunDataSuccessCallbackResult> {
  3.   return new Promise((resolve, reject) => {
  4.     wx.getWeRunData({
  5.       success: resolve,
  6.       fail: reject
  7.     });
  8.   });
  9. }
  10. // 云函数 getOpenData
  11. export const main = async (event: { cloudID: string }) => {
  12.     const { cloudID } = event;
  13.     ...
  14.     // 通过云调用直接获取开放数据
  15.     const openData = await cloud.getOpenData({
  16.       list: [cloudID]
  17.     })
  18.     return {
  19.       errCode: 0,
  20.       errMsg: '获取成功',
  21.       data: openData.list[0].data
  22.     }
  23.     ...
  24. }
复制代码
2. 云函数调用数据库存储用户步数
  1. // 小程序端
  2. await wx.cloud.callFunction({
  3.   name: 'createUserStep',
  4.     data: {
  5.       step
  6.     }
  7. });
  8. // 云函数 createUserStep
  9. const { step } = event;
  10. const wxContext = cloud.getWXContext();
  11. const openid = wxContext.OPENID;
  12. const userStepQuery = {
  13.   openid,
  14.   date: db.RegExp({
  15.     regexp: `^${getCurrentDate()}`,
  16.   })
  17. }
  18. const userStep = await userStepCollection.where(userStepQuery).get() as cloud.DB.IQueryResult;
  19. if (userStep.data.length > 0) {
  20.   userStepCollection.doc(userStep.data[0]._id!).update({ data: { step } })
  21. } else {
  22.   userStepCollection.add({
  23.     data: {
  24.       step,
  25.       openid,
  26.       date: getCurrentDate()
  27.     }
  28.   })
  29. }
  30. return userStep.data;<br><br>
复制代码
8.png

Github地址:https://github.com/greywen/MiniprogramCloudDevelopmentTemplate-TypeScript
参考:
https://www.cnblogs.com/xiabings/p/17171277.html

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册