简体   繁体   English

React-Router:使用客户端路由进行服务器请求的过程?

[英]React-Router: Process of server request with Client-Side Routing?

I'm trying to understand to client-side routing works. 我正在尝试了解客户端路由的工作方式。 But I still got a hard time to imagine how it works. 但是我仍然很难想象它是如何工作的。

Let's say I've got some code like this (for example): 假设我有这样的代码(例如):

<Router>
    <Route path="/" component={Home} />
    <Route path="/users" component={Users} />
    <Route path="/widgets" component={Widgets} />
</Router>

How is the process? 过程如何?

Are the code of the components are all downloaded from the webserver at once when I request index.html? 当我请求index.html时,是否同时从Web服务器下载了组件的代码? Then everything is managed with JavaScript on the client-side and no further requests are needed? 然后,所有内容都在客户端使用JavaScript进行管理,不需要进一步的请求了吗?

Let's say the URL ' example.com/#/users ' is called. 假设调用了网址“ example.com/#/users”。 As a consequence some part of the side is exchanged with the content of Users-component? 结果,一方的某些部分与用户组件的内容交换了?

Or are the components requested when needed? 还是在需要时要求组件? Lazy loaded so to speak. 懒惰可以这么说。

The process: The route you described imports all components handling routes, so they are all available in resulting bundle. 过程:您描述的路由会导入所有组件处理路由,因此它们在结果包中都可用。 Router matches path and renders these components accordingly (on / renders Home , on /users renders Users and so on). Router匹配路径并相应地渲染这些组件(在/渲染Home ,在/users渲染Users ,依此类推)。 They are not lazy loaded because they are already included in route definition. 它们不是延迟加载的,因为它们已经包含在路由定义中。

To have components lazy loaded you will have to use dynamic routing 要延迟加载组件,必须使用动态路由

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

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