前端各大框架路由跳转

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一样,只不过引入的包不一样而已,都是通过useSearchParamsuseParamsuseLocation进行获取

### 在 Electron 中实现前端页面路由跳转 在 Electron 应用中,前端页面路由跳转可以通过 Vue 和 Quasar 的组合来实现。具体来说,可以在 `main.js` 或者 `background.js` 文件中设置好基础环境后,在 Vue 组件里通过 Vue Router 来管理不同页面之间的导航。 对于基于 Quasar 框架的应用而言,为了使页面能够完成正常的跳转操作,需要按照如下方式配置: #### 配置 App.vue 使用 `<RouterView>` 显示动态组件 ```html <template> <q-layout view="hHh lpR fFf"> <!-- 主要布局结构 --> <router-view /> </q-layout> </template> <script setup lang="ts"></script> ``` 此部分定义了一个容器用于渲染匹配当前路径的第一个子组件[^1]。 #### 创建链接以便于用户点击触发页面切换 在一个名为 home.vue 的文件内部创建指向其他页面(比如 login 页面)的超链接按钮或者文字链: ```html <template> <div class="home-container"> <router-link :to="{ name: 'login' }">前往登录页</router-link> </div> </template> ``` 这里利用了 vue-router 提供的 `<router-link>` 标签来进行页面间的平滑过渡。 #### 设置服务器端支持单页应用(SPA) 为了让浏览器地址栏中的 URL 变化不影响实际加载的内容,还需要确保后台服务能正确处理这些请求。当使用 Express 作为 Node.js 后端时,应该像下面这样做: ```javascript const path = require('path'); app.use(express.static(path.join(__dirname, '../dist'))); // 对所有未命中静态资源的 GET 请求都返回 index.html app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, '../dist/index.html')); }); ``` 这段代码的作用是在遇到未知路径时不报错而是返回主 HTML 文档,从而允许前端 JavaScript 处理剩余逻辑并显示正确的视图[^3]。 #### 关联问题解决方案 如果希望刷新页面后仍保持原有的路由状态,则可考虑采用 hash 模式的路由模式或是借助 Vuex 存储器保存当前的状态信息,并在重新加载时恢复它[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值