当前位置:网站首页>Wechat applet - component parameter transmission, state management
Wechat applet - component parameter transmission, state management
2022-07-29 05:39:00 【Program ape runs forward】
Wechat applet bind:tap
<view wx:for="{
{deliveryManInfo}}" wx:key="index">
Set up wx:key="index" after ,view It can be used directly inside index,index Indicates the subscript of each item , from 0 Start .
<view data-index="{
{index}}" bindtap="choosed">
// In the use of bindtap When you click on an event , Set up data-index, simultaneous assignment index, Then the click event can get the corresponding click item The subscript .
// Access method
choosed(e){
let choosetTab = e.currentTarget.dataset.index
}
Click the jump
Click on
State management tools
/** * Life cycle of components : When the old and new writing exist at the same time , The new writing shall prevail , The old writing will not take effect */
// neographism ( At the same time, the new way shall prevail , The old one doesn't work )
lifetimes: {
created(){
},
attached(){
}
},
// Old style -- And data Flush , Not wrapped in lifetimes in ( At the same time, the new way shall prevail , The old one doesn't work )
created(){
},
attached(){
}
/** * The life cycle of the page on which the component resides : * show: The page where the component is located is displayed .( Such as : Switch pages , Switch tabbar etc. ) * hide: The page where the component is located is hidden .( Such as : Switch pages , Switch tabbar etc. ) * resize: When the size of the page where the component is located changes */
pageLifetimes: {
show: function () {
},
hide: function () {
},
resize: function () {
},
},
created: The component instance has just been created , You can't call setData – Just created
attached: The component is fully initialized , Enter the page node tree
This life cycle is very useful , Most initialization can be done at this time ( Such as : Initiate network request )
Put it in UI In structure , It hasn't been rendered yet
ready: Execute after the component view layer layout is completed .-- Render completed
moved: When a component instance is moved to another node tree
detached: Leave the page node tree . You can clean up some timers and other work – When it was destroyed
error: Execute whenever a component method throws an error
to subcontract


1. Manual implementation wx.request Of Promise turn
//Promise turn
requestPromise(url, method) {
return new Promise((resolve, reject) => {
wx.request({
url,
method,
success: (res) => {
resolve(res)
},
fail: (res) => {
reject(res)
}
})
})
},
// call ---- Method 1
getRequest() {
this.requestPromise('https://www.escook.cn/categories', 'GET').then(res=>console.log(res),err=>console.log(err))
}
// call ---- Method 2
getRequest() {
this.requestPromise('https://www.escook.cn/categories', 'GET').then(res=>console.log(res)).catch(err=>console.log(err))
}
// call ---- Method 3
async getRequest() {
try{
const res = await this.requestPromise('https://www.escook.cn/categories', 'GET')
console.log(res);
}catch(err){
console.log(err);
}
},



In many cases , We need to load the page at the beginning , Automatically request some initialization data . At this time, you need to enter the... On the page onLoad Calling function to get data in event
stay onLoad Receive navigation parameters in


On the surface , Components and pages are made up of .js、.json、.wxml and .wxss These four documents make up . however , Components and pages .js And .json The files are obviously different :
Component's .json Declaration required in document “component”: true attribute
Component's .js What is called in the file is Component() function
The event handler function of the component needs to be defined to methods In nodes
Parent component
// page.wxml
<tabs tabItems="{
{tabs}}" bindmytap="onMyTab" >
Content - You can put slots here
</tabs>
// page.js
data: {
tabs:[
{
name:" Experience problems "},
{
name:" goods 、 Business complaints "}
]
},
onMyTab(e){
console.log(e.detail);
},
Child components
// com.wxml
<view class="tabs">
<view class="tab_title" >
<block wx:for="{
{tabItems}}" wx:key="{
{item}}">
<view bindtap="handleItemActive" data-index="{
{index}}">{
{
item.name}}</view>
</block>
</view>
<view class="tab_content">
<slot></slot>
</view>
</view>
// com.js
Component({
properties: {
tabItems:{
type:Array,
value:[]
}
},
/** * The initial data of the component */
data: {
},
/** * A list of methods for a component */
methods: {
handleItemActive(e){
this.triggerEvent('mytap','haha');
}
}
})
wx.switchTab Jump page , Parameters cannot be carried after the path . If you need to carry parameters , Can pass getApp() function , Combined with global variables , stay onShow Get the corresponding parameters in .
Not in onLaunch Called when getCurrentPages(), here page Not yet generated .
Life cycle
onLoad(Object query) Trigger when the page loads . A page can only be called once , Can be in onLoad
Get the parameters in the path to open the current page .
onShow() Page shows / Trigger when entering the front desk .
onReady() Triggered when the page is first rendered . A page can only be called once , Representative page is ready , Can interact with view layers .
So the loading order is to load first onLoad, Then the onShow, Last onReady
边栏推荐
- paddle.fluild常量计算报错‘NoneType‘ object has no attribute ‘get_fetch_list‘
- 微信小程序视频上传组件直接上传至阿里云OSS
- 公众号不支持markdown格式文件编写怎么办?
- 组件传参与生命周期
- Common shortcut keys for Ad
- Day 2
- 365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
- H5语义化标签
- Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
- HCIA-R&S自用笔记(25)NAT技术背景、NAT类型及配置
猜你喜欢

MySQL解压版windows安装
![[C language series] - three methods to simulate the implementation of strlen library functions](/img/b2/00cd2b79adc23813088656ec3bc17e.png)
[C language series] - three methods to simulate the implementation of strlen library functions
![[C language series] - detailed explanation of file operation (Part 1)](/img/12/2d47fde0385d3f1dcb31f5efa82f7b.png)
[C language series] - detailed explanation of file operation (Part 1)

HCIA-R&S自用笔记(25)NAT技术背景、NAT类型及配置

Clickhouse learning (VIII) materialized view

Clickhouse learning (IX) Clickhouse integrating MySQL

Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction

Occt learning 003 - MFC single document project

Day 5
![[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises](/img/6f/f7c5d605ea9b7b9e7c49ac716492ef.jpg)
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises
随机推荐
【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词
Storage category
Detailed installation and use tutorial of MySQL (nanny installation with pictures and texts)
抽象类与接口
【电子电路】ADC芯片如何选型
Redirection and files
相对定位和绝对定位
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
Database operation day 6
table中同一列中合并相同项
Do students in the science class really understand the future career planning?
ClickHouse学习(十)监控运行指标
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
Integer overflow and printing
Common shortcut keys for Ad
第三课threejs全景预览房间案例
Wechat applet video upload component is directly uploaded to Alibaba cloud OSS
js简单代码判断打开页面的设备是电脑PC端或手机H5端或微信端
Terminal shell common commands
公众号不支持markdown格式文件编写怎么办?