小程序非主页面的数据动作关联主页面的数据刷新操作

发布于:2025-09-11 ⋅ 阅读:(25) ⋅ 点赞:(0)

如果在主页面跳转到其他页面。比如说我的收藏页面,然后有取消收藏的动作,当返回到主页面的时候,如果有关联数据显示在主页面,刷新页面对应的状态。
下面的代码是实现:
 

//卡片收藏/取消

if (newCollectd) {

this.setData({

'item.isCollect': 1

});

this.onCollectNet();

}

else {

this.setData({

'item.isCollect': 0

});

this.onCollectCancelNet();

}

const pages = getCurrentPages();

const currentPage = pages[pages.length - 1];

console.log('currentPage', currentPage.route)

if (currentPage.route == 'pages/collectManage/myCollectList/index') {

app.eventBus.emit('likeChange', { item: this.data.item });

}

// this.triggerEvent('action', {type: 'like', item: this.data.item });

},

在主页面的JS的代码
 

lifetimes: {

attached() {

app.eventBus.on('loginRequestCollect', (data) => {

this.getCollectList()

})

app.eventBus.on('likeChange', (data) => {

let itemInArr = false

let changeIndex = null

this.data.swiperList.forEach((v, i) => {

if (v.id === data.item.id) {

itemInArr = true

changeIndex = i

v.isCollect = data.item.isCollect

console.log(v)

}

})

if (itemInArr) {

let changeStr = 'swiperList[' + changeIndex + ']'

this.setData({ [changeStr]: this.data.swiperList[changeIndex] })

setTimeout(() => { });

}

});

}

},

通过对比看是否存在,并且获取在数组的序列,然后刷新单个数据,而不是刷新整个数组


网站公告

今日签到

点亮在社区的每一天
去签到