反应路由器而不更改URL [英] React Router Without Changing 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.
但超出此段落没有任何用法示例,我找不到在任何地方的用法,即:如何使用链接
组件导航时没有路径名,如果不是路径名,我将路由哪个参数等。
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屋!