This article mainly shares with you the quick knowledge of WeChat mini programs, which can make it easier for everyone to develop WeChat mini programs. I hope this article can help everyone.
1. Configuration
Full configuration analysis
- ##project.config.json (project configuration file)
{ // 文件描述 "description": "项目配置文件", // 项目代码配置 "setting": { // 是否检查 url 域名安全性和 TLS 版本 "urlCheck": false, // 是否将项目的 es5 代码转成 es6 "es6": true, // 是否自动补全 css 兼容前缀 "postcss": true, // 是否压缩代码 "minified": true, // 是否启用新功能 "newFeature": true }, // 编译方式 "compileType": "miniprogram", // 版本号 "libVersion": "1.7.2", // appid "appid": "touristappid", // 项目名 "projectname": "haiyangbg", // 项目配置 "condition": { // 搜索关键字 "search": { "current": -1, "list": [] }, // 客服 "conversation": { "current": -1, "list": [] }, // 编译方式 "miniprogram": { "current": -1, "list": [] } } }
- app.json (mini-program configuration)
{ // 项目路由设置(第一项为首页) "pages":[ "pages/index/index", "pages/logs/logs" ], // 窗口设置 "window":{ // 顶部导航栏背景色,必须是十六进制颜色值,如"#000000" "navigationBarBackgroundColor": "#9ef468", // 顶部导航栏显示文字 "navigationBarTitleText": "组件展示", // 导航栏文字颜色,仅支持 black/white "navigationBarTextStyle": "black", // 下拉背景的文字样式,仅支持 dark/light "backgroundTextStyle":"light", // 下拉背景色,必须是十六进制颜色值,如"#000000" "backgroundColor": "#333", // 是否开启下拉刷新 "enablePullDownRefresh": true, // 距离最底端触多少距离时触发触底事件,单位px "onReachBottomDistance": 40 }, //网络请求过期时间,单位毫秒 "networkTimeout": { // 普通ajax请求 "request": 20000, // Socket请求 "connectSocket": 20000, // 文件上传 "uploadFile": 20000, // 文件下载 "downloadFile": 20000 }, // tab导航栏 "tabBar": { // 文字的颜色 "color": "#999", // 选中时文字的颜色 "selectedColor": "#000", // 背景色 "backgroundColor": "#fff", // 上边框的颜色,仅支持 black/white "borderStyle": "black", // tab导航栏显示在底部还是顶部(顶部不显示图片) "position": "bottom", // 导航栏列表项 "list": [{ // 导航到的页面路径 "pagePath": "pages/index/index", // tab按钮上的文字 "text": "组件", // 图片路径 "iconPath": "img/com-l.png", // 选中后显示的图片 "selectedIconPath": "img/com-d.png" },{ "pagePath": "pages/logs/logs", "text": "API", "iconPath": "img/api-l.png", "selectedIconPath": "img/api-d.png" }] }, // 调试信息 "debug": true }
- page.json (single Page configuration)
json of a single page is the
window field of
app.json, when loaded into this page When
#onLaunch ------ Triggered when the mini program initialization is completed (will only be called once)
onShow ------ 1. Mini program starts 2. Enter the foreground display from the background, trigger
onHide ------ When the applet enters the background from the foreground, trigger
onError -- ---- 1. A script error occurred 2. The api call failed and was triggered with an error message
- Single page Page life cycle
- onLoad ------ Triggered when the page is loaded (will only be called once)
onReady ------ When the initial rendering is completed, trigger (will only be called once)
onHide ----- - When the page is hidden, trigger
onUnload ------ When the page is unloaded, trigger
- Details Life cycle classification:
- 1. Mini program launch:
-
App.onLaunch
- -> App.onShow
Page. onLoad
- -> Page.onShow - ->
Page.onReady2. Switch to the background (app and page life cycles overlap ):
The applet was switched to the background - ->
page.onHide
- -> App.onHide
- -> Switched back to the applet Program - -> App.onShow
- ->
3. Jump page:
old represents the previous page , new means new pagenavigateTo
Jump- -> Set the route as the target page route- -> old.onHide
- -> Initialization page data - ->
- ->
new.onShow- ->
new.onReady-
new.onReadyredirectTo
Redirect- -> Set routing- ->old.onUnload
- -> init data - ->new .onLoad
- ->new.onShow
- -> -
##reLaunch##navigateBack
Page return - -> Set routing - ->
old.onUnload- -> init data - ->
new.onShow Restart- -> Set routing- -> - old.onUnload
- -> init data - ->
new.onLoad
- ->new.onShow
- ->new.onReady
##switchTab
Tab switch (picture taken from Official document)
3. Data binding and rendering
Data Binding: { { message } }
Rendering
##List rendering:
- wx:for=" { { message } } " - wx:for-index="idx" (设置索引的变量名,默认 index ) - wx:for-item="itemName"(设置每一项的变量名,默认item )Rendering block:
- Conditional rendering:
- 4. Template )
- wx:if="boolean" - wx:elif="boolean" -wx:else="boolean" == (if - else if - if) - hidden="boolean"
Basic usage:
- (Define code snippets, which can be called in different places, use the name attribute as the name of the template, and use the is attribute when calling)
// 源码(需要和调用的页面在同一个wxml里) <template> <view> I am {{ name }} </view> </template> // 调用 <template></template> // js 数据 Page({ data: { message: { name: '海洋饼干' } } })
Advanced usage - When many pages needed the same template, the template needed to be imported
First Create a new
template folderin the pages folder, and create a new template.wxml file in the folder
// template.wxml <template> <view> I am {{ name }} </view> </template>
// page.wxml 调用 <import></import> <template></template> // js 数据 Page({ data: { message: { name: '海洋饼干' } } })
5. Event
Event list
Click Event
Click event
tap
- longpress
- event
-
-
Start moving after touching touchmove
Interrupted after touch
touchcancelTouch ended
touchend-
Animation Event -
过渡完成时触发
transitionend
动画开始时触发
animationstart
一次迭代结束时触发
animationiteration
动画完成时触发
animationend
事件冒泡,事件捕获
绑定事件 + 冒泡:
bind
+ 事件名 , 如bindtap
绑定事件 + 阻止冒泡:
catch
+ 事件名 , 如catchtap
捕获 + 冒泡:
capture-bind:
+ 事件名 , 如capture-bind:tap
捕获 + 中断事件 + 取消冒泡:
capture-catch:
+ 事件名 , 如capture-catch:tap
事件对象
BaseEvent
( 基础事件,所有事件的父类 )同 target
id
( String - 事件源的id )tagName
( String - 当前组件的类型 )dataset
( Object - 事件源组件上由data-开头的自定义属性集合 )type
( String - 事件类型 )timeStamp
( Integer - 事件生成时的时间戳 )target
( Object - 触发事件的组件的属性 )currentTarget
( Object - 当前组件的属性 )TouchEvent
( 触摸事件 )identifier
( Number - 触摸点的标识符 )pageX
( Number - 距文档左上角 x 轴的距离 )pageY
( Number 距文档左上角 y 轴的距离 )clientX
( Number 距页面可显示区域 x 轴的距离 )clientY
( Number 距页面可显示区域 y 轴的距离 )touches
( Array 停留在屏幕中的触摸点的信息对象集合 )changedTouches
( Array 变化的触摸点信息对象集合 )CustomEvent
( 自定义事件 )detail
( Object - 自定义事件额外的信息 )
6. wxs 模块
wxs 文件就是 js 文件,引入 wxs 文件就是引入一个 js 模块( 不能用es6 ),现有两种引入方式
-
在
wxml
里引用,使用<wxs></wxs>
标签
1.1.module
必填,为当前模块的模块名
1.2.src
选填,引用.wxs
文件的相对路径(仅当标签为 单闭合标签 或 标签的内容为空 时有效)
1.3. 例引入:<wxs></wxs> 自己撸: <wxs> var foo = '海洋饼干' module.exports = { foo } </wxs>
在
wxs
里引用,使用require
引用
2.1. 例var tools = require("./tools.wxs")
7. wxss 样式
微信版的 css ,几个不同的地方
添加 自适应尺寸单位 ( rpx )
添加 导入外联样式 ( @import )
精减 css选择器,只支持
类选择器 .class
id选择器 #id
标签选择器 element
兄弟选择器 element1,element2
伪元素选择器 只支持两种 ::after ::before
8. 自定义组件
使用自定义组件
-
创建自定义组件( 类似于page,但需要在 json 文件中将
component
字段设为true
){ "component": true }
-
创建组件构造器 ( 构造函数不是page(),而是Component() )
Component({ // 组件的对外属性(父组件传的数据) properties: { msg: { type: String, value: 'msg', // 父组件值改变时触发的回调 observer: () => { console.log('i am change') } } }, data: { componentData: {} }, })
-
使用自定义组件( 先要在页面的
json
文件中进行引用声明 )// 先引用声明 "usingComponents": { // hybg 标签名 "hybg": "../component/component" // 相对路径 } // 直接使用,需要的话要绑定数据 <hybg></hybg>
slot 模板
1. 单 solt " // component <view> <slot></slot> </view> // page ( hybg 是上面的组件标签名) <hybg> <view> page 的 slot </view> </hybg> // 效果 <view> <view> page 的 slot </view> </view> " 2. 多slot " // 先在 Component 的 js 中添加 options: { // 启用多slot multipleSlots: true }, // 多 slot 需要使用不同的 name 来区分 <view> <slot></slot> <slot></slot> </view> // page ( hybg 是上面的组件标签名) <hybg> <view> first -- slot </view> <view> last-- slot </view> </hybg> // 效果 <view> <view> first -- slot </view> <view> last-- slot </view> </view> "
父子组件事件(子组件传值)
// page fatherEvent: function(e){ console.log(e.detail) // 组件传递的自定义信息 } <hybg></hybg> // component tap: function(){ var myEventDetail = { a: 10} // detail对象,提供给事件监听函数 var myEventOption = {} // 触发事件的配置选项 // bubbles 事件是否冒泡 // capturePhase 事件是否拥有捕获阶段 // composed 是否可以穿越父组件边界 this.triggerEvent('hybgEvent', myEventDetail, myEventOption) } <view>触发 hybgEvent 事件</view>
相关推荐:
The above is the detailed content of Quick knowledge about WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

随着移动互联网技术和智能手机的普及,微信成为了人们生活中不可或缺的一个应用。而微信小程序则让人们可以在不需要下载安装应用的情况下,直接使用小程序来解决一些简单的需求。本文将介绍如何使用Python来开发微信小程序。一、准备工作在使用Python开发微信小程序之前,需要安装相关的Python库。这里推荐使用wxpy和itchat这两个库。wxpy是一个微信机器

小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

微信小程序是一种轻量级的应用程序,可以在微信平台上运行,不需要下载安装,方便快捷。Java语言作为一种广泛应用于企业级应用开发的语言,也可以用于微信小程序的开发。在Java语言中,可以使用SpringBoot框架和第三方工具包来开发微信小程序。下面是一个简单的微信小程序开发过程。创建微信小程序首先,需要在微信公众平台上注册一个小程序。注册成功后,可以获取到

实现思路x01服务端的建立首先,在服务端,使用socket进行消息的接受,每接受一个socket的请求,就开启一个新的线程来管理消息的分发与接受,同时,又存在一个handler来管理所有的线程,从而实现对聊天室的各种功能的处理x02客户端的建立客户端的建立就要比服务端简单多了,客户端的作用只是对消息的发送以及接受,以及按照特定的规则去输入特定的字符从而实现不同的功能的使用,因此,在客户端这里,只需要去使用两个线程,一个是专门用于接受消息,一个是专门用于发送消息的至于为什么不用一个呢,那是因为,只

PHP与小程序的地理位置定位与地图显示地理位置定位与地图显示在现代科技中已经成为了必备的功能之一。随着移动设备的普及,人们对于定位和地图显示的需求也越来越高。在开发过程中,PHP和小程序是常见的两种技术选择。本文将为大家介绍PHP与小程序中的地理位置定位与地图显示的实现方法,并附上相应的代码示例。一、PHP中的地理位置定位在PHP中,我们可以使用第三方地理位

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了如何在小程序中用公众号模板消息,下面一起来看一下,希望对大家有帮助。

随着小程序的广泛应用,越来越多的开发者需要将其与后台服务器进行数据交互,其中最常见的业务场景之一就是上传文件。本文将介绍在小程序中实现文件上传的PHP后台实现方法。一、小程序中的文件上传在小程序中实现文件上传,主要依赖于小程序APIwx.uploadFile()。该API接受一个options对象作为参数,其中包含了要上传的文件路径、需要传递的其他数据以及

苏州健康码的小程序叫“苏康码”,它是苏州市疫情防控指挥部指定的通行服务码,疫情防控期间在全市范围内通用,可以作为广大民众日常出行的重要凭证,同时作为防疫人员查验的主要依据;也是省内所有来苏逗苏人员以及在苏工作学习生活,旅游或临时停留人员申报的键康申报数据为基础,结合相关数据比对后动态生成的个人电子健康凭证。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
