微信小程序基础
常见标签
view视图
1 2
| <view>hello</view> <view>你好</view>
|
text文章
selcectable属性
1 2
| <text selectable>你好</text>
|
decode属性
1 2
| <text decode>世界 你好</text>
|
type属性
primary首选按钮
1
| <button type="primary">primary</button>
|
default默认按钮
1
| <button type="default">default</button>
|
warn警告
1
| <button type="warn">warn</button>
|
open-type属性
getPhoneNumber获取电话
1
| <button open-type="getPhoneNumber">PhoneNumber</button>
|
getUserInfo获取用户信息
1
| <button open-type="getUserInfo">getUserInfo</button>
|
contact客服
1
| <button open-type="contact">contact</button>
|
checkbox复选
1 2 3 4 5
| <checkbox-group> <checkbox value="1">女</checkbox> <checkbox value="2">男</checkbox> </checkbox-group>
|
radio单选
1 2 3 4 5
| <radio-group>
<radio value="1">女</radio> <radio value="2">男</radio> </radio-group>
|
block分块
1 2 3 4 5
| <block> <text style="margin-right: 6px;">首页</text> <text style="margin-right: 6px;">物品</text> </block>
|
1 2 3 4 5 6
| <input type="text"></input> <input password></input> <input type="number"></input> <input disabled></input> <input placeholder="请输入" placeholder-class="ph-style"></input>
|
事件绑定
bindinput相当于vue中的v-model实现双向数据绑定糖衣炮弹
1 2 3 4 5
|
<input type="text" bindinput="ChangeMe"></input> <button bindtap="clickMe" data-val1="1" data-test="2" >点击我</button> <view>{{msg}} </view>
|
1 2 3 4 5 6 7 8 9 10 11 12
| page({ data:{ msg:"原始数据" }, ChangeMe:function(){ console.log("文本框变了"); }, clickMe:function(value){ console.log(value.currentTarget.dataset); } )}
|
数据绑定
1 2 3
|
<button bindtap="clickMe2">点击我2</button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| page({ data:{ msg:"原始数据" }, clickMe2:function(event){ this.setData({ msg:"原始数据的内容和视图内容都发生了改变" }) console.log(this.data.msg); }, )}
|
微信API调用
1 2 3 4 5 6
|
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取用户信息</button> <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取电话</button> <button open-type="contact" bindcontact="getContact">联系客服</button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| page({ getUserInfo(e){ console.log(e) }, getPhoneNumber(e){ console.log(e); }, getContact(e){ console.log(e); }, })
|
富文本标签
1 2 3 4
|
<rich-text nodes="{{msg2}}"></rich-text>
|
1 2 3 4 5
| page({ data:{ msg2:"<b>加粗</b><p style='color:red'>变红</p>" }, )}
|
微信弹窗
弹窗样式
1 2
| <button bindtap="showAlert">弹窗按钮</button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
showAlert(){ wx.showToast({ title: '成功', icon:'success', duration:2000 }) wx.showLoading({ title: '加载中', duration:2000 }) wx.showModal({ title: '提示', content: '这是一个模式框', complete: (res) => { if (res.cancel) { console.log("点击了取消"); } if (res.confirm) { console.log("点击了确定"); } } }) },
|
弹窗跳转
1 2
| <button bindtap="navi">页面跳转</button>
|
1 2 3 4 5 6 7 8
| navi(){ wx.navigateTo({ url: '/pages/test1/test1?id=1', }) },
|
弹窗前后端交互
1
| <button bindtap='getData'>获取后端数据</button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| getData(){ var that=this; wx.request({ url: 'http://localhost:10737//jobs', success(res){ var data = res.data; that.setData({ jobs:data }) console.log(that.data.jobs); } }) }
|