- App.JSON
- {
- "pages": ["pages/index/index"]
- }
index.wxml
<text>Hello World</text>
index.JS
Page({})
image.PNG
- {
- "pages":[
- "pages/index/index",
- "pages/logs/logs"
- ],
- "window":{
- "backgroundTextStyle":"light",
- "navigationBarBackgroundColor": "#fff",
- "navigationBarTitleText": "WeChat",
- "navigationBarTextStyle":"black"
- }
- }
- <!--pages/wxml/index.wxml-->
- <text > 当前时间:{{time}}</text>
- // pages/wxml/index.JS
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- time: (new Date()).toString()
- },
- })
- <!-- 正确的写法 -->
- <text data-test="{{test}}"> hello world</text>
- <!-- 错误的写法 -->
- <text data-test={{test}}> hello world </text>
- <!--
- { a: 1, b: 2, c: 3 }
- -->
- <view> {{a + b}} + {{c}} + d </view>
- <!-- 输出 3 + 3 + d -->
- <view wx:if="{{condition}}"> True </view>
- <view wx:if="{{length> 5}}"> 1 </view>
- <view wx:elif="{{length> 2}}"> 2 </view>
- <view wx:else> 3 </view>
- <block wx:if="{{true}}">
- <view> view1 </view>
- <view> view2 </view>
- </block>
- <!-- array 是一个数组 -->
- <view wx:for="{{array}}">
- {{index}}: {{item.message}}
- </view>
- <!-- 对应的脚本文件
- Page({
- data: {
- array: [{
- message: 'foo',
- }, {
- message: 'bar'
- }]
- }
- })
- -->
- <view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
- {{idx}}: {{itemName.message}}
- </view>
- <block wx:for="{{[1, 2, 3]}}">
- <view> {{index}}: </view>
- <view> {{item}} </view>
- </block>
WXML 提供模板 (template), 可以在模板中定义代码片段, 然后在不同的地方调用.
- <template name="msgItem">
- <view>
- <text> {{index}}: {{msg}} </text>
- <text> Time: {{time}} </text>
- </view>
- </template>
- <template name="msgItem">
- <view>
- <text> {{index}}: {{msg}} </text>
- <text> Time: {{time}} </text>
- </view>
- </template>
- <template is="msgItem" data="{{...item}}"/>
WXML 提供两种文件引用方式 import 和 include.
- <!-- item.wxml -->
- <template name="item">
- <text>{{text}}</text>
- </template>
- <import src="item.wxml"/>
- <template is="item" data="{{text:'forbar'}}"/>
- <!-- A.wxml -->
- <template name="A">
- <text> A template </text>
- </template>
- <!-- B.wxml -->
- <import src="a.wxml"/>
- <template name="B">
- <text> B template </text>
- </template>
- <!-- C.wxml -->
- <import src="b.wxml"/>
- <template is="A"/> <!-- 这里将会触发一个警告, 因为 b 中并没有定义模板 A -->
- <template is="B"/>
include 可以将目标文件中除了 <template/> <wxs/> 外的整个代码引入
- <!-- index.wxml -->
- <include src="header.wxml" />
- <view>
- body
- </view>
- <include src="footer.wxml" />
data-* Any 自定义属性 组件上触发的事件时, 会发送给事件处理函数
bind*/catch* EventHandler 组件的事件
image.PNG
在 WXSS 中, 引入了 rpx(responsive pixel) 尺寸单位.
@import url('./test_0.css')
小程序支持动态更新内联样式:
- <!--index.wxml-->
- <!-- 可动态变化的内联样式 -->
- <!--
- {
- eleColor: 'red',
- eleFontsize: '48rpx'
- }
- -->
- <view style="color: {{eleColor}}; font-size: {{eleFontsize}}"></view>
image.PNG
官方样式库
具体使用文档可参考: https://github.com/Tencent/weui-wxss
语法
类型
语句
关键字
操作符
对象
image.PNG
- // moduleA.JS
- module.exports = function( value ){
- return value * 2;
- }
- // 在 B.JS 中引用模块 A
- var multiplyBy2 = require('./moduleA')
- var result = multiplyBy2(4)
- var common = require('common.js')
- Page({
- helloMINA: function() {
- common.sayHello('MINA')
- },
- goodbyeMINA: function() {
- common.sayGoodbye('MINA')
- }
- })
使用全局函数 getApp() 获取全局的实例
- // a.JS
- // 获取全局变量
- var global = getApp()
- global.globalValue = 'globalValue'
- // b.JS
- // 访问全局变量
- var global = getApp()
- console.log(global.globalValue) // 输出 globalValue
- // a.JS
- // 局部变量
- var localValue = 'a'
- // 获取 global 变量
- var App = getApp()
- // 修改 global 变量
- App.globalData++ // 执行后 globalData 数值为 2
- // b.JS
- // 定义另外的局部变量, 并不会影响 a.JS 中文件变量
- var localValue = 'b'
- // 如果先执行了 a.JS 这里的输出应该是 2
- console.log(getApp().globalData)
- <view>{{ msg }}</view>
- Page({
- onLoad: function () {
- this.setData({ msg: 'Hello World' })
- }
- })
image.PNG
image.PNG
image.PNG
image.PNG
image.PNG
image.PNG
image.PNG
image.PNG
- App({
- onLaunch: function (options) {
- console.log("app.js ---onLaunch---" + JSON.stringify(options));
- },
- onShow:function(){
- console.log("app.js ---onShow---");
- },
- onHide:function(){
- console.log("app.js ---onHide---");
- },
- onError: function (msg){
- console.log("app.js ---onError---" + msg);
- },
- globalData: {
- userInfo: null
- }
- })
- Page({
- onLoad: function (options) {
- console.log("page ---onLoad---");
- },
- onReady: function () {
- console.log("page ---onReady---");
- },
- onShow: function () {
- console.log("page ---onShow---");
- },
- onHide: function () {
- console.log("page ---onHide---");
- },
- onUnload: function () {
- console.log("page ---onUnload---");
- }
- })
初始化 小程序打开的第一个页面 onLoad, onShow
打开新页面 调用 API wx.navigateTo onHide onLoad, onShow
页面重定向 调用 API wx.redirectTo onUnload onLoad, onShow
页面返回 调用 API wx.navigateBack onUnload onShow
Tab 切换 调用 API wx.switchTab 请参考表 3-6 请参考表 3-6
重启动 调用 API wx.reLaunch onUnload onLoad, onShow
- wx.request({
- url: 'test.php',
- data: {},
- header: { 'content-type': 'application/json' },
- success: function(res) {
- // 收到 https 服务成功后返回
- console.log(res.data)
- },
- fail: function() {
- // 发生网络错误等情况触发
- },
- complete: function() {
- // 成功或者失败后触发
- }
- })
- <!-- page.wxml -->
- <view id="tapTest" data-hi="WeChat" bindtap="tapName"> Click me! </view>
- // page.JS
- Page({
- tapName: function(event) {
- console.log(event)
- }
- })
touchstart 手指触摸动作开始
touchmove 手指触摸后移动
touchcancel 手指触摸动作被打断, 如来电提醒, 弹窗
touchend 手指触摸动作结束
tap 手指触摸后马上离开
longpress 手指触摸后, 超过 350ms 再离开, 如果指定了事件回调函数并触发了这个事件, tap 事件将不被触发
longtap 手指触摸后, 超过 350ms 再离开 (推荐使用 longpress 事件代替)
transitionend 会在 WXSS transition 或 wx.createAnimation 动画结束后触发
animationstart 会在一个 WXSS animation 动画开始时触发
animationiteration 会在一个 WXSS animation 一次迭代结束时触发
animationend 会在一个 WXSS animation 动画完成时触发
type String 事件类型
timeStamp Integer 页面打开到触发事件所经过的毫秒数
target Object 触发事件的组件的一些属性值集合
currentTarget Object 当前组件的一些属性值集合
detail Object 额外的信息
touches Array 触摸事件, 当前停留在屏幕中的触摸点信息的数组
changedTouches Array 触摸事件, 当前变化的触摸点信息的数组
bindtap,catchtouchstart
image.PNG
https://developers.weixin.qq.com/ebook?action=get_post_info&token=935589521&volumn=1&lang=zh_CN&book=miniprogram&docid=00080e799303986b0086e605f5680a
获取微信登录凭证 code
发送 code 到开发者服务器
到微信服务器换取微信用户身份 id
绑定微信用户身份 id 和业务用户身份
业务登录凭证 SessionId
wx.getStorage/wx.getStorageSync 读取本地数据缓存
每个小程序的缓存空间上限为 10MB, 如果当前缓存已经达到 10MB, 再通过 wx.setStorage 写入缓存会触发 fail 回调.
要实现了一个购物商城的小程序, 首页是展示一堆商品的列表.
- Page({
- onLoad: function() {
- var that = this
- wx.request({
- url: 'https://test.com/getproductlist',
- success: function (res) {
- if (res.statusCode === 200) {
- that.setData({
- list: res.data.list
- })
- }
- }
- })
- }
- })
在 onLoad 发起请求前, 先检查是否有缓存过列表, 如果有的话直接渲染界面
- Page({
- onLoad: function() {
- var that = this
- var list =wx.getStorageSync("list")
- if (list) { // 本地如果有缓存列表, 提前渲染
- that.setData({
- list: list
- })
- }
- wx.request({
- url: 'https://test.com/getproductlist',
- success: function (res) {
- if (res.statusCode === 200) {
- list = res.data.list
- that.setData({ // 再次渲染列表
- list: list
- })
- wx.setStorageSync("list",list) // 覆盖缓存数据
- }
- }
- })
- }
- })
缓存用户登录态 SessionId
- //page.JS
- var App = getApp()
- Page({
- onLoad: function() {
- // 调用 wx.login 获取微信登录凭证
- wx.login({
- success: function(res) {
- // 拿到微信登录凭证之后去自己服务器换取自己的登录凭证
- wx.request({
- url: 'https://test.com/login',
- data: { code: res.code },
- success: function(res) {
- var data = res.data
- // 把 SessionId 和过期时间放在内存中的全局对象和本地缓存里边
- App.globalData.sessionId =data.sessionId
- wx.setStorageSync('SESSIONID',data.sessionId)
- // 假设登录态保持 1 天
- var expiredTime = +new Date() +1*24*60*60*1000
- App.globalData.expiredTime =expiredTime
- wx.setStorageSync('EXPIREDTIME',expiredTime)
- }
- })
- }
- })
- }
- })
- //App.JS
- App({
- onLaunch: function(options) {
- var sessionId =wx.getStorageSync('SESSIONID')
- var expiredTime =wx.getStorageSync('EXPIREDTIME')
- var now = +new Date()
- if (now - expiredTime <=1*24*60*60*1000) {
- this.globalData.sessionId = sessionId
- this.globalData.expiredTime = expiredTime
- }
- },
- globalData: {
- sessionId: null,
- expiredTime: 0
- }
- })
利用 wx.scanCode 获取二维码的数据
- //page.JS
- Page({
- // 点击 "扫码订餐" 的按钮, 触发 tapScan 回调
- tapScan: function() {
- // 调用 wx.login 获取微信登录凭证
- wx.scanCode({
- success: function(res) {
- var num = res.result // 获取到的 num 就是餐桌的编号
- }
- })
- }
- })
获取网络状态 利用 wx.getNetworkType 获取网络状态
- //page.JS
- Page({
- // 点击 "预览文档" 的按钮, 触发 tap 回调
- tap: function() {
- wx.getNetworkType({
- success: function(res) {
- // networkType 字段的有效值:
- // Wi-Fi/2g/3g/4g/unknown(Android 下不常见的网络类型)/none(无网络)
- if (res.networkType == 'wifi') {
- // 从网络上下载 PDF 文档
- wx.downloadFile({
- url:'http://test.com/somefile.pdf',
- success: function (res) {
- // 下载成功之后进行预览文档
- wx.openDocument({
- filePath: res.tempFilePath
- })
- }
- })
- } else {
- wx.showToast({ title: '当前为非 Wifi 环境' })
- }
- }
- })
- }
- })
image.PNG
image.PNG
image.PNG
image.PNG
请点赞! 因为你的鼓励是我写作的最大动力!
官方微信公众号
吹逼交流群: 711613774
吹逼交流群
来源: http://www.jianshu.com/p/ff0675330e7a