1. WXML
Grammar of WeChat Mini Program
- Binding Data
Thedata
variables wrapping braces,{ {}}
similar interpolation expressions, such as{ { message}}
- List rendering
List renderingwx:for
can render thedata
data through instructions , add it when renderingwx:key
, for unique identification, bywx:for-item
specifying the variable name of the current element of the array , and by specifying the variable name of thewx:for-index
current subscript of the array, the code example is as follows
<view wx:for="{
{ items }}" wx:key="{
{ index }}">
{
{ item }} --- {
{ index }}
</view>
data: {
message: "hello view",
otherMessage: "other message",
items: [1, 2, 3, 4],
show: false
}
- Conditional rendering
Conditional rendering can be performed bywx:if
instructions, you can alsowx:elif
,wx:else
etc. to perform conditional rendering, the code example is as follows
<view wx:if="{
{show}}">
{
{ message }}
</view>
<view wx:else="{
{!show}}">
{
{otherMessage}}
</view>
- Templates
Define code snippets in templates and then call them in different places. When defining the templatetemplate
, use thename
attribute, which is the content of the code. When using a templatetemplate
, useis
attributes to know which template is used. If you want to pass data when using the template, you can pass itdata
. The sample code is as follows
<template name="header">
<view>Header {
{message}}</view>
</template>
<template is="header" data="{
{message}}"></template>
If the data
data is an object that you want to get the value of a certain object, then use ...
, in assignment, the following sample code
<template name="header">
<view>Header {
{name}} {
{age}}</view>
</template>
<template is="header" data="{
{...mes}}"></template>
data: {
message: "hello view",
otherMessage: "other message",
items: [1, 2, 3, 4],
show: false,
mes: {
name: "张三",
age: 23
}
},
- Click event
Use the click event in the appletbindTap
, if you need to modify the value, use itthis.setData()
, and the data will change after the click. The sample code is as follows
<view wx:if="{
{show}}">
{
{ message }}
</view>
<view wx:else="{
{!show}}" bindtap="handleTap">
{
{otherMessage}}
</view>
handleTap() {
// console.log("tap")
this.setData({
otherMessage: "new message"
})
}
- Summary of some methods of WeChat applet click events:
- If it is a normal click event, then it can be used
bindtap
. The sample code is as follows:
<view class="content" bindtap="handleAddressClick">{
{address}}</view>
- If it is
radio-group
, some click events of the radio button can be usedbindchange
. The sample code is as follows:
<radio-group bindchange="handleTypeChange">
<label>
<radio value="buy" checked="true" class="buy">求购</radio>
<radio value="sell">转让</radio>
</label>
</radio-group>
- If it is
input
some click event of the input box, then it can be usedbindinput
. The sample code is as follows:
<input placeholder="填写您的具体需求" class="info-input" bindinput="handleMessageChange"></input>
- Some summary of the WeChat applet about initiating a request:
- If you want to initiate a request, can use
wx.request()
thisAPI
, for initiating a request, the following sample code:
const data = Object.assign({
}, this.staticData, {
address: this.data.address,
distinct: app.globalData.distinct
})
wx.request({
url: 'https://nuanwan.wekeji.cn/student/index.php/trade/add_item',
data: data,
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: this.handleSubmitScuuess.bind(this)
})
- A summary of some methods for obtaining the detail page in the WeChat applet:
- In the WeChat applet, if you want to obtain some detailed information of the page after clicking it, you can do it in the
onLoad()
event. In theonLoad()
life cycle function, pass in theoptions
option, getid
, and the page will be executed as soon as the page is loaded. Code example as follows:
// pages/detail/detail.js
const app = getApp()
const header = require("../../components/header/header.js")
const data = Object.assign({
}, header.data, {
address: "",
type: "",
message: "",
contact: ""
})
Page({
// data: {
// // address: "",
// // type: "",
// // message: "",
// // contact: ""
// },
data: data,
// 页面一加载的时候就会获取到相应的详情信息,通过id发起请求,获取到具体的信息
onLoad(options) {
// console.log(options)
this.getDetailInfo(options.id);
},
getDetailInfo(id) {
wx.request({
url: 'https://nuanwan.wekeji.cn/student/index.php/trade/get_item',
data: {
distinct: app.globalData.distinct,
id: id
},
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: this.getDetailSucc.bind(this)
})
},
getDetailSucc(res) {
const result = res.data.data;
this.setData({
address: result.address,
type: result.type,
message: result.message,
contact: result.contact
})
}
})