微信小程序 (1) 基础用法

1 微信开发者工具

1.1 创建小程序项目

1.2 手机预览(开发版)

预览开发版

预览体验版

1.3 常用设置

★ 新标签页打开文件: 设置 >> 编辑器设置 >> ✅总是在新标签页打开文件

1.4 快捷键设置

1.4.1 自定义快捷键

1.4.2 快捷键跨电脑复用

点击如图按钮,会显示自定义快捷键的JSON文档,将这个JSON复制

在另外一台主机同样点击此按钮,将JSON覆盖,就能实现快捷键设置的复用

bash 复制代码
// 将键绑定放在此文件中以覆盖默认值auto[]
[
  {
    "key": "ctrl+w",
    "command": "editor.action.smartSelect.expand",
    "when": "editorTextFocus"
  },
  {
    "key": "shift+alt+right",
    "command": "-editor.action.smartSelect.expand",
    "when": "editorTextFocus"
  },
  {
    "key": "ctrl+alt+left",
    "command": "workbench.action.navigateBack",
    "when": "canNavigateBack"
  },
  {
    "key": "alt+left",
    "command": "-workbench.action.navigateBack",
    "when": "canNavigateBack"
  },
  {
    "key": "ctrl+alt+right",
    "command": "workbench.action.navigateForward",
    "when": "canNavigateForward"
  },
  {
    "key": "alt+right",
    "command": "-workbench.action.navigateForward",
    "when": "canNavigateForward"
  },
  {
    "key": "ctrl+y",
    "command": "editor.action.deleteLines",
    "when": "textInputFocus && !editorReadonly"
  },
  {
    "key": "ctrl+shift+k",
    "command": "-editor.action.deleteLines",
    "when": "textInputFocus && !editorReadonly"
  },
  {
    "key": "ctrl+alt+l",
    "command": "editor.action.formatSelection",
    "when": "editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly"
  },
  {
    "key": "ctrl+k ctrl+f",
    "command": "-editor.action.formatSelection",
    "when": "editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly"
  },
  {
    "key": "ctrl+d",
    "command": "editor.action.copyLinesDownAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "shift+alt+down",
    "command": "-editor.action.copyLinesDownAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "shift+alt+up",
    "command": "editor.action.moveLinesUpAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "alt+up",
    "command": "-editor.action.moveLinesUpAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "shift+alt+down",
    "command": "editor.action.moveLinesDownAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "alt+down",
    "command": "-editor.action.moveLinesDownAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "ctrl+p",
    "command": "editor.action.triggerParameterHints",
    "when": "editorHasSignatureHelpProvider && editorTextFocus"
  },
  {
    "key": "ctrl+shift+space",
    "command": "-editor.action.triggerParameterHints",
    "when": "editorHasSignatureHelpProvider && editorTextFocus"
  },
  {
    "key": "shift+f6",
    "command": "editor.action.rename",
    "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
  },
  {
    "key": "f2",
    "command": "-editor.action.rename",
    "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
  }
]

2 版本控制

2.1 新建小程序项目和Gitee仓库

用微信开发者工具新建一个小程序项目

在Gitee新建一个空白仓库

在新建的小程序目录(与"pages目录","app.json文件"处在同一级目录),初始化Git仓库

右键选择"Git Bash Here",执行完后,会生成一个.git的隐藏文件夹

bash 复制代码
git init

2.2 项目与仓库关联(HTTPS地址)

方式一:

小程序目录,右键选择"Git Bash Here"

bash 复制代码
git remote add origin https://gitee.com/eastsuntechnology/aaa-bbb-ccc.git

验证是否关联成功,输入下面代码,出现"origin 的 fetch/push 地址",表示关联成功

bash 复制代码
git remote -v

方式二:

2.3 提交代码

配置好Gitee的帐号密码后,提交并推送

2.3 拉取代码

在本地文件夹,右键选择"Git Bash Here",克隆对应仓库

bash 复制代码
git clone https://gitee.com/eastsuntechnology/aaa-bbb-ccc.git

用"微信开发者工具"导入克隆的项目

3 重要文件

3.1 全局配置

app.json

pages: 用于记录当前小程序所有页面的路径

pages中新增页面存放路径,小程序开发者工具会自动创建对应页面文件

小程序会把排在第一位的页面当做首页

window: 全局定义小程序所有页面背景色,文字颜色等

style: 全局定义小程序组件所使用的样式版本(最新的是v2版本)

app.wxss

app.js

3.2 局部配置

3.2.1 wxml(区别于html)

标签名称: view(div) text(span) image(img) navigator(a)

属性节点: <navigator url="/pages/home/home"></navigator> //<a href=""/>

提供类类似vue的模板语法: 数据绑定 / 列表渲染 / 条件渲染

3.2.2 wxss(区别于css)

新增了rpx尺寸单位(微信规定屏幕宽度时750rpx) //微信会根据手机像素自动调节1rpx=?px

wxss只支持部分css选择器

类选择器(.class),id选择器(#id)

元素选择器(element)

并集选择器,后代选择器

伪类选择器(::after, **::**before)

3.3 渲染层和逻辑层

2层之间内存完全隔离,只能通过序列化后的JSON数据(字符串,数字,普通对象)通信(setData)

因此在html中可以直接通过"element.src = imgUrl"给img标签赋值图片

但是微信小程序的js和wxml是隔离的,在js中无法通过DOM获取image的组件对象,它们之间只能通过setData通信,即逻辑层 >> 通过setData发送JSON数据 >> 渲染层

相关推荐
马哥python说3 小时前
我用DeepSeek Harness快速上线了一个情侣小程序
微信小程序·小程序·云开发
Jackzhang3 小时前
[端与游戏开发手记] 小程序第一次调接口,卡在域名白名单的那半天
微信小程序
djxzyr7 小时前
微信小程序传感器实战:罗盘、加速计、陀螺仪与加速度实时波形
微信小程序·小程序
陈拉斯2 天前
无后端的人格测试小程序:当「数据不出设备」变成架构约束,我学到了什么
微信小程序
李纲明2 天前
微信小程序开了 Skyline 后样式全乱、滚动对不上甚至部分组件不能用?Skyline 怎么接才稳
javascript·微信小程序·小程序·devdiy
李纲明2 天前
微信小程序等位大桌被小桌占掉或过号回来就闹:先核桌型分队与过号容忍次数,再查门口屏与推送同源
微信小程序·devdiy·等位排队·桌型分队·过号重排·门口屏·推送同源
李纲明3 天前
微信小程序团课临开课才取消或空位没人补:先核取消时限与自动释放,再查候补确认与预扣回滚
微信小程序·devdiy·团课预约·取消时限·空位释放·候补确认·预扣回滚
用户034187671383 天前
用秒哒做了个拍照识餐小程序,上线两周踩的 4 个坑
微信小程序
李纲明3 天前
微信小程序租赁归还说本来就坏或配件对不上:先核借还点验与随单存证,再查差异计价公示
微信小程序·devdiy·租赁点验·借还拍照·随单存证·差异计价·押金释放
李纲明4 天前
微信小程序次卡跨店对不上账或前台随手改余次:先核流水只增不改与统一日切,再查冲正权限与总部只读
微信小程序·devdiy·核销流水·跨店次卡·日切对账·冲正权限·总部只读