useHref
根据当前位置解析完整的 URL,以用作 href
到 link
的 href
。如果提供了相对路径,它将解析为完整的 URL。
¥Resolves a full URL against the current location to be used as an href
to a link
. If a relative path is supplied, it will resolve to a full URL.
import { useHref } from "@remix-run/react";
function SomeComponent() {
const href = useHref("some/where");
return <a href={href}>Link</a>;
}
¥Signature
useHref(to, options)
to
可选。附加到已解析 URL 的路径。
¥Optional. The path to append to the resolved URL.
useResolvedPath
文档中的 Splat 路径 部分,了解 future.v3_relativeSplatPath
未来标志在 splat 路由中相对于 useHref()
行为的行为
options
唯一的选项是 { relative: "route" | "path"}
,它定义了解析相对 URL 时的行为。
¥The only option is { relative: "route" | "path"}
, which defines the behavior when resolving relative URLs.
route default — 相对于路由层级,而非 URL
¥route default — relative to the route hierarchy, not the URL
path — 使操作相对于 URL 路径,因此 ..
将删除一个 URL 段。
¥path — makes the action relative to the URL paths, so ..
will remove one URL segment.