Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of asynchronously loading routing components in html5

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the method of loading routing components asynchronously by html5". In daily operation, I believe that many people have doubts about the method of loading routing components asynchronously by html5. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the method of loading routing components asynchronously by html5". Next, please follow the editor to study!

The first thing to pay attention to is routing. We define todo-related routes in one file, and then merge all routes together in the route definition of app. The todo.routes.ts is as follows:

Then define a routing module in app.routes.ts:

Finally, this routing module is introduced into AppModule.

Angular's routing module already provides loadChildren definitions that can help us implement this function directly. Here is the new app routing definition.

Here, we leave the todo path to the TodoModule module in app/todo/todo.module. In the TodoModule module, there is already a definition of child routes.

Finally, modify app.module.ts to ensure that TodoModule is no longer introduced into it. In this way, we did not introduce any components or services of the todo module into the main module AppModule. In this way, the functions of the main module can be run without the TodoModule module completely. When the user opens the url in / todo, the LazyModule module in app/lazy/lazy.module is loaded and left to handle the url of the response.

Automatic slicing and packaging of webpack in create-react-app environment

Babel is required to support import syntax

Import ("). Then

ChildB= > console.log (ChildB)

)

Mode const Child=asyncComponent (() = > import (""))

AsyncComponent function needs to be encapsulated by itself.

Implement react-loadable through third-party plug-ins

Routing lazy loading

Principle:

Using webpack to split the code is the premise of lazy loading. Lazy loading is the asynchronous invocation of components, and it takes time to download.

Tell webpack to package the component into blocks, tell the route to trigger a function when activated, and then load the component, which will request the block code of the component when loading, and the block code will be inserted into the style of the current component. The implementation process is as follows:

1. Webpack configuration:

Output: chunkFilename:'chunks/ [name]-[chunkhash:8] .js'

Explanation: the code after build is easier to identify

2. Routing configuration:

Const home= () = > import ("")

Note: babel-plugin-syntax-dynamic-import is required for import import, and import will be promoted. Configure babelrc "plugins": ["syntax-dynamic-import"]

Explanation: webpackChunkName: "group-home" names the block | A block with the same name will be split to reduce the number of requests.

3. Register asynchronous components within the component:

Const navbar= () = > import ("")

Registration component: components: {navbar}

If navbar is not asynchronous, the code will hit app.js instead of home block or navbar block.

At this point, the study on "the method of loading routing components asynchronously by html5" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report