当前位置: 首页 > news >正文

前端各大框架路由跳转

1、uniapp

export const useRouter = (): { push: Function, replace: Function, back: Function, authRouter: Function, route: Function } => {
    type routeQuery = {
        path: string,
        query?: {
            [key: string]: string | number | undefined
        }
    }
    const push = ({ path, query }: routeQuery): void => {
        uni.navigateTo({ url: `${path}?${handle(query)}` })
    }
    const replace = ({ path, query }: routeQuery): void => {
        uni.redirectTo({ url: `${path}?${handle(query)}` })
    }
    const back = (index?: number): void => {
        const pages = getCurrentPages();
        if (pages.length > 1) {
            uni.navigateBack({ delta: index ? index : 1 })
        } else {
            uni.navigateTo({ url: '/takeStock/index/index' })
        }
    }
    const addressBack = (index?: number): void => {
        const pages = getCurrentPages();
        if (pages.length > 1) {
            uni.navigateBack({ delta: index ? index : 1 })
        } else {
            uni.navigateTo({ url: '/addressBook/index/index' })
        }
    }
    const handle = (query?: { [key: string]: string | number | undefined }): string => {
        let queryString = '';
        if (query) {
            Object.keys(query).forEach((item, index) => {
                queryString += `${item}=${query[item] || ''}&`
            })
        }
        queryString = queryString.slice(0, queryString.length - 1);
        return queryString
    }
    const route = () => {
        const page = getCurrentPages();
        return page[page.length - 1].options
    }
    const authRouter = () => {
        const page = getCurrentPages();
        if (!uni.getStorageSync("takeStockToken")) {
            uni.setStorageSync('pathRouter', {
                path: "/" + page[page.length - 1].route,
                query: page[page.length - 1].options
            });
            uni.redirectTo({ url: "/takeStock/login/index" })
        }
    }
    return { push, replace, back, authRouter, route }
}

2、Taro

export const useRouter = (): { push: Function, replace: Function, back: Function } => {
    type routeQuery = {
        path: string,
        query?: {
            [key: string]: string | number | undefined
        }
    }
    const push = ({ path, query }: routeQuery): void => {
        Taro.navigateTo({ url: `${path}?${handle(query)}` })
    }
    const replace = ({ path, query }: routeQuery): void => {
        Taro.redirectTo({ url: `${path}?${handle(query)}` })
    }
    const back = (index?: number): void => {
        const pages = getCurrentPages();
        if (pages.length > 1) {
            Taro.navigateBack({ delta: index ? index : 1 })
        } else {
            Taro.navigateTo({ url: '/pages/login/index' })
        }
    }
    const handle = (query?: { [key: string]: string | number | undefined }): string => {
        let queryString = '';
        if (query) {
            Object.keys(query).forEach((item) => {
                queryString += `${item}=${query[item] || ''}&`
            })
        }
        queryString = queryString.slice(0, queryString.length - 1);
        return queryString
    }
    // const route = () => {
    //     const page = getCurrentPages();
    //     return page[page.length - 1].options
    // }
    return { push, replace, back  }
}

3、react

const useRouter = () => {
    const navigate = useNavigate();
    type routeQuery = {
        path?: string,
        query?: {
            [key: string]: string | number | undefined
        }
    }
    // 前进
    const push = ({ path, query }: routeQuery) => {
        navigate(`${path}?${handle(query)}`)
    }
    const pushPath = (path: string) => {
        navigate(`${path}`)
    }
    // 替换
    const replace = ({ path, query }: routeQuery) => {
        navigate(`${path}?${handle(query)}`, { replace: true })
    }
    // 返回
    const back = (index?: number) => {
        history.go(Number(`-${index ? index : 1}`))
    }
    // 拼接参数
    const handle = (query?: { [key: string]: string | number | undefined }): string => {
        let queryString = '';
        if (query) {
            Object.keys(query).forEach((item, index) => {
                queryString += `${item}=${query[item] || ''}&`
            })
        }
        queryString = queryString.slice(0, queryString.length - 1);
        return queryString
    }
    return { push, replace, back, pushPath }
}
export default useRouter;

获取参数:
 

//?id=123
const params = new URLSearchParams(useLocation().search);
params.get("id")


import { useSearchParams } from "react-router-dom";
const [searchParams] = useSearchParams();
searchParams.get('id')




//:id
import { useParams } from 'react-router-dom';
const params = useParams() as { id: string };
params.id

4、vue

import { Modal } from "ant-design-vue";
import { useRoute, useRouter } from "vue-router";


const router = useRouter();
const route = useRoute();
router.push({
    path: "/workPortal/myProcess/details/" + row.id,
    query: {
      routeTitle: row.processName.split('-')[0] + " - " + row.sourceId
    }
  });


route.params.id
route.query.id

5、nextjs

import { useRouter } from 'next/navigation'
const router = useRouter();
router.push("/from/records/detail?id=" + row.sysId)





//?id=123
import { useSearchParams } from 'next/navigation'
const searchParams = useSearchParams();
searchParams.get("id")







//:id
import { useParams } from 'next/navigation'
const params = useParams();
params.id

 6、umi

import { history } from "@umijs/max"

const useRouter = () => {
    type routeQuery = {
        path?: string,
        query?: {
            [key: string]: string | number | undefined
        }
    }
    // 前进
    const push = ({ path, query }: routeQuery) => {
        history.push(`${path}?${handle(query)}`)
    }
    const pushPath = (path: string) => {
        history.push(`${path}`)
    }
    // 替换
    const replace = ({ path, query }: routeQuery) => {
        history.replace(`${path}?${handle(query)}`, { replace: true })
    }
    // 返回
    const back = (index?: number) => {
        history.go(Number(`-${index ? index : 1}`))
    }
    // 拼接参数
    const handle = (query?: { [key: string]: string | number | undefined }): string => {
        let queryString = '';
        if (query) {
            Object.keys(query).forEach((item, index) => {
                queryString += `${item}=${query[item] || ''}&`
            })
        }
        queryString = queryString.slice(0, queryString.length - 1);
        return queryString
    }
    return { push, replace, back, pushPath }
}
export default useRouter;

参数获取和react一样,只不过引入的包不一样而已,都是通过useSearchParams、useParams、useLocation进行获取

相关文章:

  • 抖音小程序多少钱上海seo
  • 网站建设三网合一上海网站建设哪家好
  • 何如做外贸网站推网360外链
  • 网站咨询弹窗怎么做外贸推广具体是做什么
  • 我自己做的一个网站显示证书错误你就知道
  • 苏州到深圳物流公司搜索引擎seo是什么
  • 【leetcode hot 100 438】找到字符串中所有字母异位词
  • Burp Suite Professional 2024版本安装激活指南
  • 鸿蒙HarmonyOS 开发简介
  • 数据库MySQL,在终端输入后,提示不是内部命令等
  • 【Redis】Redis 入门
  • NO.21十六届蓝桥杯备战|一维数组|范围for|memset|memcpy(C++)
  • 数据库复习
  • unity pico开发 三 移动 旋转 传送
  • Python深度学习环境配置(Pytorch、CUDA、cuDNN),包括Anaconda搭配Pycharm的环境搭建以及基础使用教程(保姆级教程,适合小白、深度学习零基础入门)
  • 经验总结:使用vue3测试后端接口的模板
  • 【京准时钟】网络时间同步服务器对数据库的重要性
  • 【JAVA】阿里云百炼平台对接DeepSeek-V3大模型使用详解
  • DOM Node
  • 鸿蒙NEXT开发-Navigation组件导航
  • Python - Python操作Redis
  • JavaWeb后端基础(1)
  • 【Linux】Linux的基本指令(2)
  • 数据库数据恢复—SQL Server附加数据库报错“错误 823”怎么办?
  • PHP面试题--后端部分
  • 浅谈人工智能之Windows安装llama factory