小程序官方地址,小程序开发者工具,点击此处下载。在微信小程序中有一个配置文件project.config.json
,此文件能够让开发者在不一样设备中进行开发。css
微信小程序共支持5种文件,wxml
,wxss
,js
,json
,wxs
以及图片文件
等。每一页面都具备wxml
,wxss
,js
,json
文件,但比不是必须的,小程序和网页相似,一种以html+css+js
,而小程序则是wxml+wxss+js
,如wxml
用来描述页面结构,wxss
用例描述页面的样式,js
用来添加逻辑信息的。html
wxml
用来构建页面的结构json
//数据绑定 <!--wxml--> <view> {{message}} </view>
// page.js Page({ data: { message: 'Hello world!' } })
学会用数据绑定,wxml
中的动态数据均可来自对应的Page
的data
中的数据,如何绑定是很简单易懂的。如小程序
// 使用{{ ... }} <view> {{ message }} </view> <view id="item-{{id}}"> </view> // 对应中的Page的data数据 Page({ data: { id: 0 } }) <view wx:if="{{condition}}"> </view> Page({ data: { condition: true } })
true:表明真值。
false:表明假值。微信小程序
// 不可少的 {{ ... }} <checkbox checked="{{ture}}"> </checkbox> <view hidden="{{flag ? true : false}}"> Hidden </view>
// 必备掌握 <view>{{object.key}} {{array[0]}}{{array[1]}}</view> Page({ data: { object: { key: 'Hello ' }, array: ['hh','da'] } })
// 数组 <view wx:for="{{[zero, 1, 2, 3]}}"> {{item}} </view> Page({ data: { zero: 0 } }) // item,条目
<template is="object" data="{{for: a, bar: b}}"></template> Page({ data: { a: 1, b: 2 } }) // {for: 1, bar: 2}
// 用...来将一个对象展开 <template is="object" data="{{...obj1, ...obj2, e: 5}}"></template> Page({ data: { obj1: { a: 1, b: 2 }, obj2: { c: 3, d: 4 } } }) // {a: 1, b: 2, c: 3, d: 4, e: 5}
<template is="object" data="{{foo, bar}}"></template> Page({ data: { foo: 'my-foo', bar: 'my-bar' } }) // {foo: 'my-foo', bar:'my-bar'}
// 后边的会覆盖前面 <template is="object" data="{{...obj1, ...obj2, a, c: 6}}"></template> Page({ data: { obj1: { a: 1, b: 2 }, obj2: { b: 3, c: 4 }, a: 5 } }) // {a: 5, b: 3, c: 6}
列表的渲染,经过wx:for,下标变量名为 index,数组当前项的变量名为 item。api
<view wx:for="{{array}}"> {{index}}: {{item.message}} </view> Page({ data: { array: [{ message: 'foo', }, { message: 'bar' }] } })
wx:for-item wx:for-index
<view wx:for="{{array}}" wx:for-index="indexcoding" wx:for-item="itemcoding"> {{indexcoding}}: {{itemcoding.message}} </view>
// wx:if="{{condition}}" wx:else wx:elif <view wx:if="{{length > 5}}"> 1 </view> <view wx:elif="{{length > 2}}"> 2 </view> <view wx:else> 3 </view>
wx:if 为 false,框架什么也不作,只有为真的时候才开始局部渲染。
wx:if 有更高的切换消耗而, hidden 有更高的初始渲染消耗。数组
// 模板 template <template is=" ... " data="{{...item}}"/> Page({ data: { item: { msg: 'this is a template' } } })
// 事件 bindtap="tapName" Page({ tapName: function(event) { console.log(event) } })
事件分类:(即区分父节点有事件也会被响应)微信
<view bindtap="add"> {{count}} </view> Page({ data: { count: 1 }, add: function(e) { this.setData({ count: this.data.count + 1 }) } })
<import src="item.wxml"/> <template is="item" data="{{text: 'forbar'}}"/>
<!-- index.wxml --> <include src="header.wxml"/> <view> body </view> <include src="footer.wxml"/> <!-- header.wxml --> <view> header </view> <!-- footer.wxml --> <view> footer </view>
wxss
与css
大部分相同,但wxss
对css
进行了修改和补充,wxss
的特性有尺寸单位
和样式导入
,在小程序中你会看到rpx
这样的尺寸单位,小程序开发初期是以iPhone 6
标准的,固定750rpx
为屏幕宽度。app
尺寸单位,屏幕宽固定为750rpx
,在iPhone6
上,屏幕宽度为375px
,换算750rpx = 375px = 750
物理像素。iPhone6
中,1rpx = 0.5px
。框架
样式导入,使用@import
语句,路径为相对路径;如:
/** index.wxss **/ .name { padding:5px; } /** app.wxss **/ @import "index.wxss"; .age { padding:15px; }
.class #id element ::after ::before
在js
中文件运用到API
的调用,点击传送门
不用立刻懂,别作项目别懂就行。
App()
用来注册一个小程序,接受一个object
参数。
onLaunch 监听小程序初始化 onShow 监听小程序显示 onHide 监听小程序隐藏
getApp()
用来获取到小程序实例。
var app = getApp() console.log(app.globalData)
onLoad 监听页面加载 onShow 监听页面显示 onReady 监听页面初次渲染完成 onHide 监听页面隐藏 onUnload 监听页面卸载
前台、后台定义:击左上角关闭或者按了Home
键离开,进入了后台,只有当小程序进入后台必定时间,或者系统资源占用太高,才会被销毁。
下面我将继续对小程序
中的其余知识 深刻讲解 ,有兴趣能够继续关注
小礼物走一走 or 点赞