在不更改 URL 的情况下响应路由器 [英] React Router Without Changing URL

查看:31
本文介绍了在不更改 URL 的情况下响应路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要无需更改 URL 即可运行的路由.

I need to have routing that works without changing the URL.

在我自己实现这个之前,我试图通过反应路由器寻找一些东西.看到有个东西叫createMemoryHistory:

Before implementing this on my own, I tried to look for something by react router. I saw that there is such a thing called createMemoryHistory:

createMemoryHistory([options])

createMemoryHistory([options])

createMemoryHistory 创建一个内存中的历史对象,它不会与浏览器 URL 交互.这在您需要时很有用自定义用于服务器端渲染的历史对象,用于自动化测试,或者当你不想操纵浏览器 URL,例如当您的应用程序嵌入 iframe 时.

createMemoryHistory creates an in-memory history object that does not interact with the browser URL. This is useful for when you need to customize the history object used for server-side rendering, for automated testing, or for when you do not want to manipulate the browser URL, such as when your application is embedded in an iframe.

但是在这一段之外没有任何用法示例,我在任何地方都找不到它的用法,即:如何使用 Link 组件在没有路径名的情况下导航,通过哪个参数执行我路由如果不是路径名等

But beyond this paragraph there aren't any usage examples and I can't find a usage for this anywhere, i.e: how to use Link component to navigate without a pathname, by which parameter do I route if not pathname, etc.

这是否适合我的需求,还是我必须自己实现路由器?

Is this right for my needs, or do I have to implement a router on my own?

推荐答案

MemoryHistory 是一个历史提供者",你可以像这样提供给 React Router:

MemoryHistory is a "history provider", which you can supply to React Router like this:

const memoryHistory = createMemoryHistory(options);

// In your Router configuration
<Router history={memoryHistory} routes={routes} />

除了初始配置之外,其他一切都应该与常规浏览器历史记录完全相同.

Beyond the initial configuration, everything else should work exactly the same as with regular browser history.

本文介绍了如何在 React Router 中使用不同的提供者:历史

This article describes how to use different providers with React Router: Histories

这篇关于在不更改 URL 的情况下响应路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆