微信小程序-基础语法

WGenji Lv4

wxml模板语法

数据绑定

类似于vue

1
2
3
4
data(){
str:''
}
//在js里写
1
2
<view>{{str}}</view>
//在wxml里写

属性绑定

1
2
3
data(){
src:''
}
1
<image src="{{src}}"></image>

条件渲染

wx:if 和 hidden

1
2
3
4
5
6
7
<block	wx:if="{{true}}">
<view>123</view>
</block>
// block只是一个容器,本身不会被渲染,类似于template

<view hideen="flag"></view>
//hideen控制元素的显示和隐藏(display)

列表渲染

1
2
3
4
<view wx:for="{{arr}}" wx:key="index">
<view>{{item}}</view>
</view>
// item指当前项

事件绑定

image-20220924091111140

常用事件

image-20220924091232368

事件对象的属性列表

image-20220924091454110

target和currentTarget的区别

image-20220924091841618

数据赋值

1
2
3
4
5
btnTap(e){
this.setData({
count: this.data.count + 1
})
}

事件传参

image-20220924093424262

bindtap使用data-*的方式传参

1
2
3
<button bindtap='btnTap' data-info='{{msg}}'>
</button>
// 回调函数 e.target.dataset.info里获取 msg

bindinput使用value

1
2
3
<button bindtap='btnTap'value='{{msg}}'>
</button>
// 回调函数 e.detail.value里获取 msg

全局配置

window

image-20220924223609546

tabBar

最少两个,最多5个页签,tabbar页面在pages必须写在前面

组成部分

image-20220924225136505

tabBar配置项

image-20220924225550309

每个tab的配置项

image-20220924225635462

网络数据请求

出于安全性方面的考虑,只能请求https,必须将接口的域名添加到信任列表里

image-20220925124137694

image-20220925124658341

发送请求

1
2
3
4
5
6
7
8
9
10
wx.request({
url: '',
method: '',
data:{
//传参的数据
},
success:(res)=>{
console.log(res)
}
})
  • Title: 微信小程序-基础语法
  • Author: WGenji
  • Created at : 2024-08-26 13:51:51
  • Updated at : 2024-08-26 13:51:51
  • Link: https://redefine.ohevan.com/2024/08/26/微信小程序/2.基础语法/
  • License: This work is licensed under CC BY-NC-SA 4.0.