路由
Routing
基础知识
The Basics
路由驱动着大多数网站。路由管理器(Router)是对“给定这个 URL,页面上应该显示什么?”这一问题的回答。
Routing drives most websites. A router is the answer to the question, “Given this URL, what should appear on the page?”
一个 URL 由许多部分组成。例如,URL https://my-cool-blog.com/blog/search?q=Search#results 包含:
A URL consists of many parts. For example, the URL https://my-cool-blog.com/blog/search?q=Search#results consists of
-
一个 协议 (scheme):
https -
a scheme:
https -
一个 域名 (domain):
my-cool-blog.com -
a domain:
my-cool-blog.com -
一个 路径 (path):
/blog/search -
a path:
/blog/search -
一个 查询 (query)(或 搜索 (search)):
?q=Search -
a query (or search):
?q=Search -
一个 哈希 (hash):
#results -
a hash:
#results
Leptos 路由管理器处理路径和查询(/blog/search?q=Search)。给定这段 URL,应用程序应该在页面上渲染什么?
The Leptos Router works with the path and query (/blog/search?q=Search). Given this piece of the URL, what should the app render on the page?
核心理念
The Philosophy
在大多数情况下,路径应该驱动页面上显示的内容。从用户的角度来看,对于大多数应用程序,应用状态的大多数主要变化都应该反映在 URL 中。如果你复制并粘贴该 URL 并在另一个标签页中打开,你应该会发现自己或多或少处于同一个位置。
In most cases, the path should drive what is displayed on the page. From the user’s perspective, for most applications, most major changes in the state of the app should be reflected in the URL. If you copy and paste the URL and open it in another tab, you should find yourself more or less in the same place.
从这个意义上说,路由管理器实际上是你应用程序全局状态管理的核心。它比任何其他东西都更能驱动页面上显示的内容。
In this sense, the router is really at the heart of the global state management for your application. More than anything else, it drives what is displayed on the page.
路由管理器通过将当前位置映射到特定的组件,为你处理了大部分此类工作。
The router handles most of this work for you by mapping the current location to particular components.