Lazy Modules(懒加载模块)
懒加载模块从Framework7版本3.4.0开始提供。
懒加载模块可以通过仅在首页/视图所需的功能加载时加载,以加快Web应用程序的启动时间,并在导航到使用它们的页面时加载其他模块/组件。这将使您的初始应用程序脚本和样式文件的大小更小,这在构建Web应用程序或PWA时非常重要。
Framework7提供两种类型的模块:ES模块和“浏览器模块”。要使用ES模块,您需要使用支持import/export的打包工具,如Webpack或Rollup。浏览器模块仅用于在不使用任何打包工具时使用。
Modules API
要在Framework7初始化后加载模块,我们需要使用以下应用程序方法:
app.loadModule(module) - 加载模块
- module - 以下之一:
- object with Framework7 Plugin
- function that returns Framework7 Plugin
- string 带有要加载的模块名称(例如,'searchbar')
- string 带有要加载的模块路径(例如,'path/to/components/searchbar.js')
Method returns Promise
app.loadModules(modules) - 加载模块
- modules - 包含模块的数组,其中每个数组项是上述描述的之一
Method returns Promise
ES Modules
只有在使用类似Webpack或Rollup的打包工具时,此方法才有效。
首先,我们需要了解我们的应用程序需要哪些模块来显示初始页面,并将它们导入:
// import core framework with core components only:
import Framework7 from 'framework7';
// import framework7 modules/components we need on initial page
import Searchbar from 'framework7/components/searchbar';
import Accordion from 'framework7/components/accordion';
// install core modules
Framework7.use([Searchbar, Accordion]);
// init app
var app = new Framework7({
// f7 params
});Later when we need to install additional F7 module we can use dynamic imports:
import('framework7/components/gauge')
.then(module => app.loadModule(module.default))
.then(() => {
// module loaded and we can use gauge api
app.gauge.create(/* ... */)
})If we need to load few modules at a time:
Promise
.all([
import('framework7/components/gauge'),
import('framework7/components/calendar')
])
.then((modules) => {
// loaded module will be at ".default" prop of import result
var modulesToLoad = modules.map(module => module.default);
return app.loadModules(modulesToLoad);
})
.then(() => {
// modules loaded and we can use their api
app.gauge.create(/* ... */)
app.calendar.create(/* ... */)
})It may be not very convenient to write it every time so we can make a function for that:
function loadF7Modules(moduleNames) {
var modulesToLoad = moduleNames.map((moduleName) => {
return import(`framework7/components/${moduleName}`);
});
return Promise.all(modulesToLoad)
.then((modules) => {
return app.loadModules(modules.map(module => module.default));
})
}And we can use it like:
loadF7Modules(['gauge', 'calendar']).then(() => {
// modules loaded and we can use their api
app.gauge.create(/* ... */)
app.calendar.create(/* ... */)
});If we need to preload modules for specific route then route's async is the best fit for it:
var routes = [
{
path: '/',
url: './index.html',
},
/* Page where we need Gauge and Calendar modules to be loaded */
{
path: '/gauge-calendar/',
async: function ({ resolve }) {
// load modules
loadF7Modules(['gauge', 'calendar']).then(() => {
// resolve route
resolve({
componentUrl: './gauge-calendar.html',
});
});
}
}
]The following ES-module components are available for importing (all other components are part of the core):
| Component | Path |
|---|---|
| Dialog | framework7/components/dialog |
| Popup | framework7/components/popup |
| LoginScreen | framework7/components/login-screen |
| Popover | framework7/components/popover |
| Actions | framework7/components/actions |
| Sheet | framework7/components/sheet |
| Toast | framework7/components/toast |
| Preloader | framework7/components/preloader |
| Progressbar | framework7/components/progressbar |
| Sortable | framework7/components/sortable |
| Swipeout | framework7/components/swipeout |
| Accordion | framework7/components/accordion |
| ContactsList | framework7/components/contacts-list |
| VirtualList | framework7/components/virtual-list |
| ListIndex | framework7/components/list-index |
| Timeline | framework7/components/timeline |
| Tabs | framework7/components/tabs |
| Panel | framework7/components/panel |
| Card | framework7/components/card |
| Chip | framework7/components/chip |
| Form | framework7/components/form |
| Input | framework7/components/input |
| Checkbox | framework7/components/checkbox |
| Radio | framework7/components/radio |
| Toggle | framework7/components/toggle |
| Range | framework7/components/range |
| Stepper | framework7/components/stepper |
| SmartSelect | framework7/components/smart-select |
| Grid | framework7/components/grid |
| Calendar | framework7/components/calendar |
| Picker | framework7/components/picker |
| InfiniteScroll | framework7/components/infinite-scroll |
| PullToRefresh | framework7/components/pull-to-refresh |
| DataTable | framework7/components/data-table |
| Fab | framework7/components/fab |
| Searchbar | framework7/components/searchbar |
| Messages | framework7/components/messages |
| Messagebar | framework7/components/messagebar |
| Swiper | framework7/components/swiper |
| PhotoBrowser | framework7/components/photo-browser |
| Notification | framework7/components/notification |
| Autocomplete | framework7/components/autocomplete |
| Tooltip | framework7/components/tooltip |
| Gauge | framework7/components/gauge |
| Skeleton | framework7/components/skeleton |
| Pie Chart | framework7/components/pie-chart |
| Area Chart | framework7/components/area-chart |
| Typography | framework7/components/typography |
| Text Editor | framework7/components/text-editor |
| Breadcrumbs | framework7/components/breadcrumbs |