vscode开发 vue3+ts 的 uni-app 微信小程序项目(黑马)

本文详细介绍了如何使用TypeScript开发uni-app项目,包括设置VSCode环境、配置自动导入组件、安装类型声明文件、处理json注释错误,以及在微信小程序中集成Pinia的持久化存储。
摘要由CSDN通过智能技术生成

创建uni-app项目:

# 创建用ts开发的uni-app
npx degit dcloudio/uni-preset-vue#vite-ts 项目名称
# 创建用js开发的uni-app
npx degit dcloudio/uni-preset-vue#vite 项目名称

VS Code 配置

为什么选择 VS Code ?

  • HbuilderX 对 TS 类型支持暂不完善
  • VS Code 对 TS 类型支持友好,熟悉的编辑器

1.将创建好的项目用vscode打开

2.安装uni-app插件:
在这里插入图片描述

安装 uni-ui 组件库:

pnpm i @dcloudio/uni-ui

配置自动导入组件:

// pages.json
{
  // 组件自动导入
  "easycom": {
    "autoscan": true,
    "custom": {
      // uni-ui 规则如下配置  // [!code ++]
      "^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue" // [!code ++]
    }
  },
  "pages": [
    // …省略
  ]
}

3.安装类型声明文件

pnpm i -D @types/wechat-miniprogram @uni-helper/uni-app-types

如下表示安装完成:
在这里插入图片描述

4.配置tsconfig.json

{
  "extends": "@vue/tsconfig/tsconfig.json",
  "compilerOptions": {
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    },
    "lib": ["esnext", "dom"],
    "types": ["@dcloudio/types",
    "@types/wechat-miniprogram", // 配置1
    "@uni-helper/uni-app-types"] // 配置2
  },
  "vueCompilerOptions": {
    "nativeTags": ["block", "component", "template", "slot"] // 配置3
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}

5.解决vscode中json文件注释报错问题
在vscode设置中搜索文件关联,点击添加项,然后配置如下两项
在这里插入图片描述

编译和运行 uni-app 项目

在这里插入图片描述

1.在manifest.json中添加微信小程序appid

/* 小程序特有相关 */
    "mp-weixin" : {
        "appid" : "XXXXXX",
        "setting" : {
            "urlCheck" : false
        },
        "usingComponents" : true
    },

2.安装依赖

pnpm install

3.编译

pnpm dev:mp-weixin

编译完成后项目中多了一个dist目录

4.导入微信开发者工具
打开 微信开发者工具, 导入 dist\dev\mp-weixin 运行。

其他配置

小程序端 Pinia 持久化

说明:Pinia 用法与 Vue3 项目完全一致,uni-app 项目仅需解决持久化插件兼容性问题。

持久化存储插件:

pnpm i pinia-plugin-persistedstate

插件默认使用 localStorage 实现持久化,小程序端不兼容,需要替换持久化 API。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以使用uni-app的canvas组件来实现电子签名。以下是实现步骤: 1. 在uni-app项目中安装canvas组件:`npm install --save uni-canvas` 2. 在需要使用电子签名的页面中引入canvas组件: ```vue <template> <view> <canvas :canvas-id="canvasId" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"></canvas> <button @click="clearCanvas">清除</button> <button @click="saveCanvas">保存</button> </view> </template> <script> import uniCanvas from 'uni-canvas'; export default { components: { uniCanvas }, data() { return { canvasId: 'myCanvas', ctx: null, isDrawing: false, lastX: 0, lastY: 0 } }, mounted() { this.ctx = uni.createCanvasContext(this.canvasId, this); }, methods: { handleTouchStart(e) { this.isDrawing = true; this.lastX = e.touches[0].x; this.lastY = e.touches[0].y; }, handleTouchMove(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.touches[0].x, e.touches[0].y); this.ctx.stroke(); this.lastX = e.touches[0].x; this.lastY = e.touches[0].y; }, handleTouchEnd() { this.isDrawing = false; }, clearCanvas() { this.ctx.clearRect(0, 0, uni.upx2px(375), uni.upx2px(500)); }, saveCanvas() { uni.canvasToTempFilePath({ canvasId: this.canvasId, success: function(res) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function() { uni.showToast({ title: '保存成功' }); } }); } }, this); } } } ``` 在模板中,我们使用canvas组件来绘制电子签名,同时添加了两个按钮,一个用于清除画布,一个用于保存签名。在脚本中,我们使用uni.createCanvasContext方法获取canvas上下文,然后监听touch事件来绘制签名。最后,我们使用uni.canvasToTempFilePath方法将canvas转换为图片,并使用uni.saveImageToPhotosAlbum方法将图片保存到相册中。 3. 在tsconfig.json文件中添加以下配置: ```json { "compilerOptions": { "target": "es6", "module": "esnext", "moduleResolution": "node", "strict": true, "jsx": "preserve", "sourceMap": true, "resolveJsonModule": true, "esModuleInterop": true, "lib": [ "esnext", "dom", "dom.iterable", "scripthost" ] }, "include": [ "src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx" ], "exclude": [ "node_modules" ] } ``` 在tsconfig.json文件中,我们将target设置为es6,module设置为esnext,同时开启了strict模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值