[英]How to use window.location with Tauri?
I've got an onclick link in an svg that would link to another part of the website/program.我在 svg 中有一个 onclick 链接,可以链接到网站/程序的另一部分。 I used window.location.replace(...) in the onclick我在 onclick 中使用了 window.location.replace(...)
<example onClick(() => window.location.replace(...)) />
and that works fine for the web browser - but it causes the Tauri application to just white screen.这适用于网络浏览器 - 但它会导致 Tauri 应用程序只是白屏。
I did a little searching and found something that said to use window.eval(window.location.replace(...))
but that didn't work either unfortunately.我做了一些搜索,发现了一些说要使用window.eval(window.location.replace(...))
的东西,但不幸的是,这也没有用。
I was hoping to use a <Link>
as a replacement as that seems better from what I've read, but I couldn't get it working in the onClick.我希望使用<Link>
作为替代品,因为从我读过的内容来看这似乎更好,但我无法让它在 onClick 中工作。
Is there a way to use window.location functions in tauri?有没有办法在 tauri 中使用 window.location 函数? And if not is there a way I could use a <Link>
in an onClick?如果没有,我可以在 onClick 中使用<Link>
吗?
Thanks!谢谢!
If you are using react-router
and using an element's onClick
prop then you'll want to use the history
object or navigate
function to issue an imperative redirect.如果您正在使用react-router
并使用元素的onClick
道具,那么您将需要使用history
对象或navigate
功能来发出命令式重定向。 The Link
component needs to be rendered into the DOM in order for it to be clicked, it can't be used in the onClick
callback of another element. Link
组件需要渲染到 DOM 中才能被点击,它不能在另一个元素的onClick
回调中使用。
react-router@5
Import and use the useHistory
hook:导入并使用useHistory
钩子:
import { useHistory } from 'react-router';
const Component = () => {
...
const history = useHistory();
...
<example onClick={() => history.replace(".....")} />
react-router@6
Import and use the useNavigate
hook:导入并使用useNavigate
钩子:
import { useNavigate } from 'react-router';
const Component = () => {
...
const navigate = useNavigate();
...
<example onClick={() => navigate(".....", { replace: true })} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.