要想显示对应的小程序页面,一定要在app.json中的pages里面写入路径。
在小程序view里面输出值使用标签{{ }}输出
单位换算:1px=2rpx;(以iphone6为视觉稿,iphone6设计稿为750X1334)
1、for循环
<view wx:for="{{array}}"></view>或者<block wx:for="{{array}}"></block>。默认key为index,value为item。自定义key名字wx:for-index="k",自定义value名字wx:for-item="v"。
例:
<view wx:for="{{array}}" wx:for-index="k" wx:for-item="v">
{{k}}: {{v.message}}//输出二维数字
</view>
注:wx:for里面可以嵌套wx:for
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i">
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j">
<view wx:if="{{i <= j}}">
{{i}} * {{j}} = {{i * j}}
</view>
</view>
</view>
2、if判断
1)、类似if ($a){} 判断
<view wx:if="{{array}}"> True </view>
2)、类似 if($a){}else{}判断
<view wx:if="{{array[0]!=1}}"> True </view>
<view wx:else>false</view>或<view wx:else=’ array[0]==1’>false</view>
3)、类似if($a==1){}else if($a==2){}判断
<view wx:if="{{array[0]==1}}"> 1 </view>
<view wx:elif="{{array[0]==2}}"> 2 </view>
<view wx:else>3</view>
3、使用模板(优势和具体情景未理解)
可以在模板中定义代码片段,然后在不同的地方调用。感觉有点象smarty视图中的<{assign}>。还可以让其他页面引用对应的模板。
<template name="msgItem">
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template>
<template is="msgItem" data="{{...item}}"/>
4、事件
以bind或catch开头,然后跟上事件的类型:bindtap。bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡。
1)、bindtap点击事件类似js的click。
<view id="tapTest" data-hi="WeChat" bindtap="tapName"> Click me! </view>
Js:
Page({
tapName: function(event) {
console.log(event)
}
})
2)、获取前端的值
target触发事件的源组件。currentTarget事件绑定的当前组件。
① 、currentTarget 事件属性返回其监听器触发事件的节点,即当前处理该事件的元素、文档或窗口。获得点击事件所在标签的参数值(获得data-apl值)。
<button data-apl="88" bindtap='clickLog'>日志</button>
clickLog: function (event){
var a=event.currentTarget.dataset.apl;
console.log(a);
},
②、target指的是该对象里的子对象。获得点击事件下的参数值(即data-awa的值)
<view id="acd" data-awaa="884" tagName="text" bindtap='clickLog'>
<button data-apl="88" data-awa="8814" >日志</button>
</view>
clickLog: function (e){
var a=e. target.dataset.awaa;
console.log(a);
},
3)、获取前端其他标签的值,非点击标签
5、引用
1)、import
在当前页面引用对应页面的模板内容。对应模板赋值的值在当前页面赋值。import 有作用域的概念,即只会 import 目标文件中定义的 template,而不会 import 目标文件 import 的 template。
import可以在该文件中使用目标文件定义的template,如:
在 item.wxml 中定义了一个叫item的template:
<!-- item.wxml -->
<template name="item">
<text>{{text}}</text>
</template>
在 index.wxml 中引用了 item.wxml,就可以使用item模板:
<import src="item.wxml"/>
<template is="item" data="{{text: 'forbar'}}"/>
2)、include
include 可以将目标文件除了 <template/> <wxs/> 外的整个代码引入,相当于是拷贝到 include 位置,如:
<!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/>
<!-- header.wxml -->
<view> header </view>
<!-- footer.wxml -->
<view> footer </view>
6、总的app.json
1)、pages
新建的每个wxml页面都要在pages里面引用。
{
"pages":[
"pages/weather/weather",
"pages/about/about",
"pages/city/city"
],
}
7、JS各个属性的使用
1)、在其他函数,修改data中的text值。实际输出结果为“Set some data for updating view.”
setData函数用于将数据从逻辑层发送到视图层(异步),同时改变对应的 this.data 的值(同步)。
Page({
data: {
text: "This is page data."
},
ceshi:function(){
this.setData({
text:
'Set some data for updating view.'
}
}
})
2)、修改wxml文件中的css
对应的css样式在page-data中先定义个变量,在对应地方把要改的css样式传给变量,在wxml页面中在输出变量
例:修改class为search不显示
<view class='search' style='{{searchChange1}}'>
<input type='text' value='' placeholder='搜索' data-num="1" bindtap="seachClick"/>
<view class='searchimg'>
<image></image>
</view>
</view>
Page({
data: {
imgurl:"/images/index",
searchChange1:"",
searchChange2: "display:none"
},
seachClick:function(e){
var num = e.currentTarget.dataset.num;
if(num==1){
this.setData({
searchChange1: "display:none",
searchChange2: ""
});
}else{
this.setData({
searchChange1: "",
searchChange2: "display:none"
});
}
}
})
3)、获取input的值
①、bindinput
input输入的时候触发,值获取:e.detail.value。这个类似于input的keyup或者keydown事件处理方式。这个可以作为js直接获取input单个值。
例:
<view>
<input type='text' style='border:1px' bindinput='unamIn'></input>
<button bindtap="listenerLogin">登录</button>
</view>
unamIn:function(e){
var uname = e.detail.value;
console.log(uname);
}
②、bindblur
input 输入焦点时触发。值获取:e.detail.value。注意,这个值只有失去焦点的时候才会被触发,失去焦点事件和提交事件几乎同是发生,会造成button提交过去的是一个空值。解决办法:提交事件加个500毫秒的定时器
③、blindcomfirm
bindconfirm 点击完成按钮时触发。值获取:e.detail.value。这个按钮是键盘中的右下角完成按钮,使用这个事件,input类型只能是text。数据类键盘没有这个按钮。
④、bindsubmit
bindsubmit事件可以获取单个或者一直表单的值。值获取:e.detail.value.[name]。
4)、打印获得值的类型
console.log(typeof num);
5)、把this赋值给变量that,因为当调用微信接口时在微信接口使用this会认为是接口而不是当前的js文件。
例:拨打电话
dialing:function(e){
var tel = e.currentTarget.dataset.tel;
var that=this;
wx.makePhoneCall({
phoneNumber: tel,
success:function(msg){
that.setData({
popup: "1",
popupCont: "电话呼出中。。。"
});
}, fail:function(error){
that.setData({
popup: "1",
popupCont: "调用微信拨打电话接口失败!"
});
}
})
}
6)、定时器,2秒后隐藏或者调用hideDialing方法
setTimeout(function () {
that.hideDialing();
}, 2000);
7)、引用同一个js中的另外方法
otherA:function(){
var that=this;
that. hideDialing();
}
hideDialing:function(){
var that=this;
that.setData({
popup: "0",
popupCont: ""
});
}
7)、引用根目录js
//app.js 根目录js
App({
globaData:'huangenai'
})
//test.js
var app = getApp();
Page({
onLoad: function () {
console.log(app.globaData);
}
})
8)、引用其他目录的js,使用require
//common.js
function myfunc() {
console.log("myfunc....");
}
//开始调用
var app;
var common = require("../../common.js");
Page({
data:{
version:’’
},
onLoad:function() {
this.setData({version: ‘appName’});
common.myfunc(); //最后我们需要执行才能生效!
}
})
9)、this.setData({name:”1”}) name做成变量
①、
Page({
Data:{
name:”0”,
shuju:”2”;
},
ceshi:function(e){
Var c=e. currentTarget.dataset.name;//获得的只是shuju
this.setData({
[c]:”3”;
});
}
})
data中的shuju的值就变为3;
②、如若是要修改数组或者对象中的某一个值:先用一个变量,把(数组或者对象中的某一个值)用字符串拼接起来,再将变量写在[]里面即可。
data: {
info:[ {
name:"yuki",
tou:"../img/head.jpg",
zGong:130,
gMoney:222222
}]
}
onLoad: function () {
var that=this;
var up = "info[" + 0 + "].gMoney";//先用一个变量,把(info[0].gMoney)用字符串拼接起来
that.setData({
[up]:1
})
}
10)、data数据类型(字符串、数字、数组、对象)
data: {
text: 'init data',
num: 0,
array: [{text: 'init data'},{text: 'init data'}],
object: {
text: 'init data',
text1: 'init data'
}
},
11)、类似ajax请求(wx. Request())
注: header[‘content-type’]默认为 'application/json';如果method为GET则可以使用默认,method为POST则使用content-type:application/x-www-form-urlencoded
wx.request({
url: '',
data:{
a:'',
a:''
},
method:"POST",
header: { 'content-type': 'application/x-www-form-urlencoded' },
success:function(res){
},
fail:function(e){
}
});
12)、上拉加载(注:1、按页查询读取,但是在js中要使用合并函数concat把新查询到的数据和旧数据合并,然后全部值赋值给对应的data里面。2、每次加载都从0到要加载的数据查询)
1)、onReachBottom
onReachBottom: function () {//上拉加载
console.log("wq");
this.setData({
count: [1,2,2,2,2,5,5,5,5,5]
})
},
2)、scroll-view的scrolltolower 事件
13)、下拉刷新
1)、onPullDownRefresh
在json中设置"enablePullDownRefresh":true
{
"enablePullDownRefresh":true
}
Js中设置:
onPullDownRefresh: function () {//下拉刷新
// wx.showNavigationBarLoading(); //在标题栏中显示加载
this.setData({
count: [1]
});
// wx.hideNavigationBarLoading(); //完成停止加载
// wx.stopPullDownRefresh(); //停止下拉刷新
},
2)、scroll-view的scrolltoupper事件
14)、js获取超链接带的参数的值
超链接:/packageA/pages/card/card?a=1&b=2&c=3
获取:在onLoad: function (options) 中会用options.对应的参数名
例:获取超链接a的值
onLoad: function (options) {
console.log(options.a);//打印出来a的值为1
},
15)、uploadFile上传中文乱码或者为空
- wx.uploadFile({
- url: 'https://' + app.globalData.host + '/api/?sign=' + sign,
- filePath: tempFilePaths[0],
- name: 'upload',
- header: {
- "content-type": "multipart/form-data",
- "content-type": "application/x-www-form-urlencoded"
- },
- formData: formData,
- success: function (res) {
16)、点击一个内容想获取这个内容标签的值和上一个内容表标签的值,使用bind*类型的事件(bind会向上冒泡)并且事件名一样。
例:点击“冒泡”同时获取uid和uname的值
<view bindtap=”userClick” data-uid=”1”>
<view bindtap=”userClick” data-uid=”1”>
冒泡
</view>
</view>
userClick:function(e){
var uid = e. currentTarget.dataset.uid;
var uname = e. currentTarget.dataset.uname;
}
注:相当于两个userClick事件,都回去获取uid和uname的值,必然会有一个是undefined,所以要做undefined判断。
17)、上传图片、上传视频注意事项
1、请求的域名必须是https协议
2、在php处理图片的时候图片保存的路径一定要绝对路径,相对路劲不行。
18)、查看小程序已经授权的内容
wx.openSetting({
success:(res)=>{
console(res);
}
});
19)、取data里的值
that.data.message
20)、获取当前可使用窗口高度
wx.getSystemInfo获取系统信息:手机品牌、手机型号、设备像素比、屏幕宽度、屏幕高度、可使用窗口宽度、可使用窗口高度、状态栏的高度、微信设置的语言、微信版本号、操作系统版本、客户端平台、用户字体大小设置、客户端基础库版本。
wx.getSystemInfo({
success: function (res) {
console.log(res. windowHeight)
});
}
});
8、总的wxss
1)、wxss中不能使用backgroup-image属性,要使用的话对应的背景图片只能在wxml中引用。
9、WXML
1)、超链接(open-type跳转类型,每个类型对应功能参考API)
注意:不能跳tabBar对应的页面,要跳tabBar对应页面要使用wx.switchTab。
<navigator url=’’ open-type=’’></navigator>
2)、block 并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性。
因为 wx:if 是一个控制属性,需要将它添加到一个标签上。如果要一次性判断多个组件标签,可以使用一个 <block/> 标签将多个组件包装起来,并在上边使用 wx:if 控制属性。
<block wx:if="{{true}}">
<view> view1 </view>
<view> view2 </view>
</block>
3)、修改input中的placeholder颜色,使用placeholder-class
<input type='text' placeholder-class='phcolor' class='inp' placeholder='职业'></input>
.phcolor{
color: #999;
}
9、微信小程序踩坑之路
1)、超出swiper组件的内容无法显示,所以无论是图片还是文在都要在swiper里面显示。
2)、video在swiper组件中是无法随着swiper滚动而变化。
解决办法:视频做成弹层或者视频另起一页。