简体   繁体   English

使用定位标记通过React Router哈希定位链接到同一页面

[英]Use anchor tag to link to same page using react router hashlocation

Trying to link to the same page with anchor tag but as i am using react router HashLocation as below, router catches it preventing the anchor to work as normal in addition of producing error of "No route matches path". 尝试链接到具有锚标记的同一页面,但由于我使用如下所示的React Router HashLocation,路由器会捕获到它,从而阻止锚正常运行,并产生“没有路线与路径匹配”错误。

Router.run(routes, Router.HashLocation, (Root) => { React.render(<Root/>, document.body); });

Same Problem has been asked in the link below with some hint of using "HistoryLocation" but i want to stick with "HashLocation" and those link do not provide concrete answer, thus need help: 在下面的链接中也提出了同样的问题,并提出了一些使用“ HistoryLocation”的提示,但是我想坚持使用“ HashLocation”,这些链接未提供具体答案,因此需要帮助:

1) How to use normal anchor links with react-router 2) Using React-Router to link within a page 1) 如何在react-router中使用普通锚链接 2) 使用React-Router在页面内链接

I wonder if there are some kind of filter in router to exclude some hash so that i can use default same page anchor linking. 我想知道路由器中是否有某种过滤器来排除一些哈希,以便我可以使用默认的同一页面锚链接。

The whole point of using the HashLocation router is to use the # to handle routing, it's a bit of a hack to allow client side navigation, but in fact it's a Single Page Application. 使用HashLocation路由器的全部HashLocation是使用#来处理路由,允许客户端导航有点HashLocation ,但实际上它是一个单页应用程序。

You can't have more than one # in your url, it makes no sense for browsers to have multiple ones. 您的网址中不能包含多个# ,对于浏览器来说,使用多个是没有意义的。 With that in mind, I think you should handle the "in page" navigation (but in fact your whole site is the "in page navigation") yourself, maybe simply using some jQuery (or pure javascript) code to scroll to the specified DOM id maybe on some hook after react-router transition. 考虑到这一点,我认为您应该自己处理“页内”导航(但实际上您的整个网站都是“页内导航”),也许只是使用一些jQuery(或纯JavaScript)代码滚动到指定的DOM id可能在react-router转换后处于某种状态。

尝试这样的事情。

    componentDidUpdate() { if ((this.props.index == this.props.selected)) React.findDOMNode(this).scrollIntoView(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM