当前位置:网站首页>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
边栏推荐
猜你喜欢

关于局部变量
![[electronic circuit] how to select ADC chip](/img/41/05153704986117ce9adaa09cb71a7a.png)
[electronic circuit] how to select ADC chip

365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13

ClickHouse学习(七)表查询优化

【C语言系列】— 把同学弄糊涂的 “常量” 与 “变量”

·来一篇编程之路的自我介绍吧·

Detailed explanation of GPIO input and output

【C语言系列】— 字符串+部分转义字符详解+注释小技巧

【电子电路】ADC芯片如何选型

Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
随机推荐
ClickHouse学习(五)集群操作
Day 5
Qtcreator+cmake compiler settings
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
Longest string without duplicate characters
B - 识别浮点常量问题
Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
个人学习笔记
ANSI C type qualifier
HCIA-R&S自用笔记(26)PPP
Day 3
Terminal shell common commands
Summary of the first week
【C语言系列】— 一道递归小题目
微信小程序更改属性值-setData-双向绑定-model
解决表单校验提示信息不消失问题以及赋值不生效问题
Common shortcut keys for Ad
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
Talking about Servlet
B - identify floating point constant problems