static-import
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStatic Import
静态导入
The keyword allows us to import code that has been exported by another module. By default, all modules we're statically importing get added to the initial bundle. A module that is imported by using the default ES2015 import syntax, , is statically imported.
importimport module from 'module'Let's look at an example! A simple chat app contains a component, in which we're statically importing and rendering three components: , a , and a to type and send messages! Within the module, we're statically importing an component to be able to show the user the emoji picker when the user toggles the emoji.
ChatUserProfileChatListChatInputChatInputEmojiPickerimportimport module from 'module'让我们来看一个例子!一个简单的聊天应用包含一个组件,我们在其中静态导入并渲染三个组件:、以及用于输入和发送消息的!在模块中,我们静态导入了一个组件,以便在用户切换表情时向其显示表情选择器。
ChatUserProfileChatListChatInputChatInputEmojiPickerWhen to Use
使用场景
- Use this for importing modules that are needed immediately on page load
- This is the default import mechanism — understand it to know when to switch to dynamic imports
- 用于导入页面加载时立即需要的模块
- 这是默认的导入机制——理解它有助于判断何时切换为动态导入
Instructions
使用说明
- Use static imports for modules critical to the initial render
- Be aware that all statically imported modules are bundled into the initial bundle
- Consider switching to dynamic imports for modules not needed on initial render
- 对初始渲染至关重要的模块使用静态导入
- 请注意,所有静态导入的模块都会被打包到初始打包文件中
- 对于初始渲染不需要的模块,可以考虑切换为动态导入
Details
细节说明
The modules get executed as soon as the engine reaches the line on which we import them.
Since the components were statically imported, Webpack bundled the modules into the initial bundle:
bash
Asset Size Chunks Chunk Names
main.bundle.js 1.5 MiB main [emitted] mainOur chat application's source code gets bundled into one bundle: . A large bundle size can affect the loading time of our application significantly depending on the user's device and network connection. Before the component is able to render its contents to the user's screen, it first has to load and parse all modules.
main.bundle.jsAppLuckily, there are many ways to speed up the loading time! We don't always have to import all modules at once: maybe there are some modules that should only get rendered based on user interaction, like the in this case, or rendered further down the page. Instead of importing all component statically, we can dynamically import the modules after the component has rendered its contents and the user is able to interact with our application.
EmojiPickerApp模块会在引擎执行到导入语句的那一刻立即运行。
由于这些组件是静态导入的,Webpack会将它们打包到初始打包文件中:
bash
Asset Size Chunks Chunk Names
main.bundle.js 1.5 MiB main [emitted] main我们的聊天应用源代码被打包为一个文件:。打包文件过大会严重影响应用的加载时间,具体取决于用户的设备和网络连接情况。在组件能够将内容渲染到用户屏幕之前,它必须先加载并解析所有模块。
main.bundle.jsApp幸运的是,有很多方法可以加快加载速度!我们不必一次性导入所有模块:有些模块可能只需要在用户交互时才渲染,比如本例中的,或者在页面滚动到下方时才渲染。我们可以在组件渲染完成、用户能够与应用交互后,动态导入这些模块,而不是静态导入所有组件。
EmojiPickerApp