首页 > 其他分享 >使用递归解决嵌套页面的状态改变

使用递归解决嵌套页面的状态改变

时间:2024-01-23 18:15:06浏览次数:17  
标签:jumpOther const 递归 idx 嵌套 window pageStatus updatePageStatus 页面

场景

一个注销页,里面有四种状态。

  1. 注销说明页
  2. 输入手机号码和图形验证码
  3. 输入短信验证码
  4. 注销处理中

在每一个状态中,都需要被APP调用window.jumpOther()返回到上一个状态

<template>
    <div v-if="pageStatus.isDelete"></div>
    <div v-if="pageStatus.isInputPhone"></div>
    <div v-if="pageStatus.isInputSms"></div>
    <div v-if="pageStatus.isSuccess"></div>
</template>
<script setup>
const pageStatus = reactive({
	isDelete: true,
	isInputPhone: false,
	isInputSms: false,
	isSuccess: false
})
</script>

 问题

一开始使用一层一层手写的方式去进行window.jumpOther()内容改写,导致代码不符合开闭原则

如果这个单页面有大量的状态需要嵌套,这种方法明显是不可行的的

window.jumpOther = () => {
    updatePageStatus(pageStatus, 'isInputPhone')
    window.jumpOther = () => {
        updatePageStatus(pageStatus, 'isDelete')
        window.jumpOther = () => {
            goSetting()
        }
    }
}

思路和解决

由于是嵌套,很容易想到递归的思想,利用递归去拼接我们需要的嵌套window.jumpOther()

可以先给出一个状态数组,里面存放了每个状态的名称和对应的jumpOther()方法

const pages = [
    {
        pageName: 'isDelete',
        jumpOther: () => {
            console.log(1)
        },
    },
    {
        pageName: 'isInputPhone',
        jumpOther: () => {
            updatePageStatus(pageStatus, 'isDelete')
            console.log(2)
        },
    },
    {
        pageName: 'isInputSms',
        jumpOther: () => {
            updatePageStatus(pageStatus, 'isInputPhone')
            console.log(3)
        },
    },
    {
        pageName: 'isSuccess',
        jumpOther: () => {
            updatePageStatus(pageStatus, 'isInputSms')
            console.log(4)
        },
    },
]
  • 首先我的想法是找到当前状态的位置,然后从当前位置做递减,将所有前列jumpOther进行嵌套。但是这样并不好实现,从而改为从头开始,向上嵌套。
  • 然后通过记录当前的window.jumpOther()作为上一次的jumpOther,作为参数传给下一次调用,这样下一次调用将参数(上一次的jumpOther)拼接到自身下
  • 传入一个idx作为标识符用来与当前状态的位置作比较,同时作为跳出递归的条件
const updatePageStatus = (statusList, updatedPage) => {
    Object.keys(statusList).forEach(s => {
        statusList[s] = false
    })
    statusList[updatedPage] = true
}
const handleSetJumpOther = (lastJump, pageRange, idx) => {
    if (pageRange === 0) {
        window.jumpOther = pages[idx].jumpOther
        return
    }
    if (idx === pageRange) return
    window.jumpOther = () => {
        pages[idx].jumpOther()
        window.jumpOther = lastJump
    }
    idx += 1
    handleSetJumpOther(window.jumpOther, pageRange, idx)
}
handleSetJumpOther(pages[0].jumpOther, pageRange, 0)

存在问题

  1.  第一个参数意义为上一次的jumpOther,第一次调用的时候应该传一个空值。但是实际上需要传第一个状态的jumpOther

完整代码

const pageStatus = {
    isDelete: false,
    isInputPhone: false,
    isInputSms: false,
    isSuccess: true,
}

const pages = [
    {
        pageName: 'isDelete',
        jumpOther: () => {
            console.log(1)
        },
    },
    {
        pageName: 'isInputPhone',
        jumpOther: () => {
            updatePageStatus(pageStatus, 'isDelete')
            console.log(2)
        },
    },
    {
        pageName: 'isInputSms',
        jumpOther: () => {
            updatePageStatus(pageStatus, 'isInputPhone')
            console.log(3)
        },
    },
    {
        pageName: 'isSuccess',
        jumpOther: () => {
            updatePageStatus(pageStatus, 'isInputSms')
            console.log(4)
        },
    },
]

const findCurrentPageRange = () => {
    const currentPage = Object.keys(pageStatus).find(p => {
        return pageStatus[p] === true
    })
    return pages.findIndex(p => {
        return p.pageName === currentPage
    })
}

const updatePageStatus = (statusList, updatedPage) => {
    Object.keys(statusList).forEach(s => {
        statusList[s] = false
    })
    statusList[updatedPage] = true
}

const handleSetJumpOther = (lastJump, pageRange, idx) => {
    if (pageRange === 0) {
        window.jumpOther = pages[idx].jumpOther
        return
    }
    if (idx === pageRange) return
    window.jumpOther = () => {
        pages[idx].jumpOther()
        window.jumpOther = lastJump
    }
    idx += 1
    handleSetJumpOther(window.jumpOther, pageRange, idx)
}

handleSetJumpOther(pages[0].jumpOther, findCurrentPageRange(), 0)

 

标签:jumpOther,const,递归,idx,嵌套,window,pageStatus,updatePageStatus,页面
From: https://www.cnblogs.com/karle/p/17983048

相关文章

  • esxi开启嵌套虚拟化
     错误:在物理机安装esxi,在该esxi上部署次级esxi主机,在次级esxi创建系统会出现以下错误   解决方法:开启嵌套虚拟化功能如果是多台esxi,则需要做多次首先将对应的次级esxi主机关机开启物理层esxi的ssh连接~#vim-cmdvmsvc/getallvms  #查看虚拟机信息10    172......
  • 【8.0】死锁和递归锁
    【一】死锁【1】介绍死锁是指两个或多个进程,在执行过程中,因争夺资源而造成了互相等待的一种现象。即两个或多个进程持有各自的锁并试图获取对方持有的锁,从而导致被阻塞,不能向前执行,最终形成僵局。在这种情况下,系统资源利用率极低,系统处于一种死循环状态。【2】示例f......
  • SQL构建表层次关系,递归累加数据
     构建表的上下级关系      有一个需求,表中数据没有关系,如同一个类型的,有多个出库时间。代码--构建表的上下级关系--可以对同一个产品的,有层次关系--使用ROW_NUMBER(),来构建,最上上一级为0INSERTINTOStock([no]--编号,[quantity]......
  • 使用 FreeMarker 生成静态页面
    在项目开发中,对于一些访问量较大的页面,可以提前基于数据生成静态页面,当数据有变化时再重新生成并更新静态页面,这样可以减轻数据库压力,提高网站的并发访问效率。常用的技术就是使用FreeMarker模板引擎,它是一款高性能的,基于模板和数据,生成输出文本的通用工具。本篇博客基于FreeM......
  • 无涯教程-CodeIgniter - 页面重定向
    在构建Web应用程序时,无涯教程经常需要将用户从一个页面重定向到另一页面。redirect()函数用于此目的。语法redirect($uri='',$method='auto',$code=NULL)参数$uri(string)     -URI字符串$method(string)-重定向方法("auto","location"或"refresh")$......
  • 无涯教程-CodeIgniter - 页面缓存
    缓存页面将提高页面加载速度。缓存的文件存储在application/cache文件夹中。启用缓存时,需要设置缓存时间,时间过后,将自动被删除。启用缓存可以通过在控制器的任何方法中执行以下行来启用缓存。$this->output->cache($n);其中$n是分钟数,您希望页面在刷新之间保持高速缓存。......
  • 遍历二叉树非递归实现
    实现1.前序遍历publicvoidpreOrderNor(TreeNoderoot){if(root==null){return;}Stack<TreeNode>stack=newStack<>();stack.push(root);while(!stack.isEmpty()){TreeNodecur......
  • 一个十分有趣的文档页面
    我的博客里有一个devops页面,专门用来汇总我写过的一些DevOps运维自动化相关的技术文章,页面很简单,就是一段文字描述加上一堆的文章链接,像下面这个样子一直以来这个页面都安安静静的存在着,访问者甚少,像是一个默默无闻没人关注的孩子,躲在角落里,偶尔有人来看上两眼,也会因为他的丑陋......
  • ohos.router (页面路由) 框架
    UIAbility概述UIAbility是一种包含用户界面的应用组件,主要用于和用户进行交互。UIAbility也是系统调度的单元,为应用提供窗口在其中绘制界面。每一个UIAbility实例,都对应于一个最近任务列表中的任务。一个应用可以有一个UIAbility,也可以有多个UIAbility,如下图所示。例如浏览器......
  • 小程序开发:修复了两个问题和页面改名
    今天发现首页的笔记本的徽标数位置不一致,如果笔记本标题长了,徽标数也会挤到其它位置去,如下: 很明显两个徽标的位置不一致,html结构如下: 将徽标和标题调整如下: 再看看页面: 解决!还有一个问题是新增所思所想如果内容区只有html元素,如:img、换行等都被视为html......