惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
A
About on SuperTechFans
量子位
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
U
Unit 42
D
DataBreaches.Net
博客园 - Franky
D
Docker
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - Blog

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
微信小程序跨页面传值-zodream梦想开源/个人编程日记
zodream · 2020-10-12 · via zodream梦想开源/个人编程日记

微信小程序跨页面传值

需求:跳转到另一个页面选择一些东西然后显示在页面中 例如:进入筛选项页面,选择,然后回到列表页面刷新页面。

第一种方法

app.js 里的 app.globalData 上设一个属性,进行传值共享

例如:

设置一个可共享的属性 data

App({
    globalData: {
        data: {}
    }
})

12345

第二个页面 设置值

Page({

    tapBack() {
        getApp().globalData.data = {};
        wx.navigateBack();
    }
})

1234567

第一个页面获取值

Page({

    onShow() {
        const data = getApp().globalData.data;
    }
})

123456

这种方法简单常用,但是使用不灵活,而且可能需要清理 getApp().globalData 上的属性,

第二种方法

直接通过 wx.navigateBack 调用页面上的方法进行回调

wx.navigateBack({
    success() {
        setTimeout(() => {
            let page = getCurrentPages().pop(); 
            if (!page) {
                return; 
            }
            // TODO
        }, 10);
    }
});

1234567891011

这里需要注意 getCurrentPages().pop() 获取到的页面需要加上延迟,不然在真机上会获取到的是未返回的页面

例如:

第二个页面 返回值

Page({

    tapBack() {
        wx.navigateBack({
            success() {
                setTimeout(() => {
                    let page = getCurrentPages().pop(); 
                    if (!page) {
                        return; 
                    }
                    page.selectedBack({});
                }, 10);
            }
        });
    }
})

12345678910111213141516

第一个页面接收值

Page({

    selectedBack(data) {
        // TODO
    }
})

123456

转载请保留原文链接: https://zodream.cn/blog/id/187.html