• 【快应用】二级页面如何携带参数返回一级页面?


     【关键词】

    快应用、router、onBackPress

    【问题背景】

    页面栈有A、B两个页面,怎么在B页面中调A页面的方法或传参?场景是:A页面是列表页,B页面是详情页,B页面状态改变后返回A页面状态也要改变。

    【解决方法】

    在onBackPress里重写返回逻辑,通过router.push方式携带参数跳转,A页面在onshow里通过this.param获取(param是push传参的参数名)。

    代码如下:

    A页面:

    1. <style>
    2. .container {
    3. flex-direction: column;
    4. justify-content: center;
    5. align-items: center;
    6. }
    7. .btn {
    8. width: 140px;
    9. height: 60px;
    10. background-color: #00bfff;
    11. border-radius: 28px;
    12. }
    13. .title {
    14. font-size: 50px;
    15. }
    16. style>
    17. <script>
    18. import router from '@system.router'
    19. module.exports = {
    20. data: {
    21. componentData: {},
    22. message: 'Hello World'
    23. },
    24. onInit() {
    25. this.$page.setTitleBar({
    26. text: 'PageA',
    27. textColor: '#ffffff',
    28. backgroundColor: '#007DFF',
    29. backgroundOpacity: 0.5,
    30. menu: true
    31. });
    32. },
    33. onShow(options) {
    34. '// Do something when show.'
    35. console.log(this.messageB + 'this.messageB')
    36. // messageB是B页面携带返回的参数
    37. this.message = this.messageB || this.message
    38. },
    39. onClick() {
    40. router.push({
    41. uri: '/pageB',
    42. params: {message: this.message}
    43. })
    44. }
    45. }
    46. script>

    B页面:

    1. <style>
    2. .container {
    3. flex-direction: column;
    4. justify-content: center;
    5. align-items: center;
    6. }
    7. .btn {
    8. width: 300px;
    9. height: 60px;
    10. margin-bottom: 20px;
    11. background-color: #00bfff;
    12. border-radius: 75px;
    13. }
    14. .title {
    15. font-size: 50px;
    16. }
    17. style>
    18. <script>
    19. import router from '@system.router'
    20. module.exports = {
    21. data: {
    22. componentData: {},
    23. messageB: ''
    24. },
    25. onInit() {
    26. this.$page.setTitleBar({
    27. text: 'PageB',
    28. textColor: '#ffffff',
    29. backgroundColor: '#007DFF',
    30. backgroundOpacity: 0.5,
    31. menu: true
    32. });
    33. },
    34. onShow(options) {
    35. '// Do something when show.'
    36. console.log(this.message + 'this.messageA')
    37. // message 是A页面传过来的值
    38. this.messageB = this.message
    39. },
    40. onClick() {
    41. this.messageB = 'Hello QuickApp'
    42. },
    43. onBackPress() {
    44. console.info(`Triggered:onBackPress`);
    45. router.clear()
    46. router.push({
    47. uri: '/hello',
    48. params: {messageB: this.messageB}
    49. })
    50. }
    51. }
    52. script>

    效果图如下:

    cke_1751.pngcke_2374.pngcke_3071.pngcke_3884.png

  • 相关阅读:
    ES6之函数的扩展
    【JMeter】定时器分类以及场景介绍
    SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.20 ActiveMQ 安装
    竞赛选题 python 机器视觉 车牌识别 - opencv 深度学习 机器学习
    支持CAN FD的Kvaser PCIEcan 4xCAN v2编码: 73-30130-01414-5如何应用?
    Apache-Doris基础概念
    抖音小店3个月做到30万的玩法,0基础也能轻松上手
    Git中的 fork, clone,branch
    Golang骚操作——使用runtime私有函数
    栈和堆什么意思,Rust所有权机制又是什么
  • 原文地址:https://blog.csdn.net/Mayism123/article/details/132718220