Lazy Modules
Lazy ModulesはFramework7バージョン3.4.0から利用可能です。
Lazy Modulesは、ホームページ/ビューに必要な機能のみを最初にロードし、それらを使用するページに移動するときに追加のモジュール/コンポーネントをロードすることで、Webアプリの起動時間を大幅に高速化する方法を提供します。これにより、初期アプリのスクリプトとスタイルが大幅に小さくなり、WebアプリまたはPWAを構築する際に非常に重要になります。
Framework7では、ESモジュールと「ブラウザモジュール」の2種類のモジュールが利用できます。ESモジュールを使用するには、WebpackやRollupのようなimport/export
をサポートするバンドラーを使用する必要があります。ブラウザモジュールは、バンドラーを使用しない場合にのみ使用するように設計されています。
モジュールAPI
Framework7のモジュールを初期化後にロードするには、次のアプリメソッドを使用する必要があります。
app.loadModule(module) - モジュールをロード
- module - 次のいずれか
- Framework7プラグインを持つオブジェクト
- Framework7プラグインを返す関数
- ロードするモジュール名を持つ文字列 (例:'searchbar'
)
- ロードするモジュールパスを持つ文字列 (例:'path/to/components/searchbar.js'
)
メソッドはPromiseを返します
app.loadModules(modules) - モジュールをロード
- modules - 上記で説明したモジュールを含む配列
メソッドはPromiseを返します
ESモジュール
このメソッドは、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
});
後で追加のF7モジュールをインストールする必要がある場合は、動的インポートを使用できます。
import('framework7/components/gauge')
.then(module => app.loadModule(module.default))
.then(() => {
// module loaded and we can use gauge api
app.gauge.create(/* ... */)
})
一度にいくつかのモジュールをロードする必要がある場合
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(/* ... */)
})
毎回記述するのは面倒な場合があるので、そのための関数を作成できます。
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));
})
}
そして、次のように使用できます。
loadF7Modules(['gauge', 'calendar']).then(() => {
// modules loaded and we can use their api
app.gauge.create(/* ... */)
app.calendar.create(/* ... */)
});
特定のルートに対してモジュールをプリロードする必要がある場合は、ルートのasync
が最適です。
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',
});
});
}
}
]
以下のESモジュールコンポーネントをインポートできます(他のすべてのコンポーネントはコアの一部です)。
コンポーネント | パス |
---|---|
ダイアログ | framework7/components/dialog |
ポップアップ | framework7/components/popup |
LoginScreen | framework7/components/login-screen |
ポップオーバー | framework7/components/popover |
アクション | framework7/components/actions |
シート | framework7/components/sheet |
トースト | framework7/components/toast |
プリローダー | framework7/components/preloader |
プログレスバー | framework7/components/progressbar |
ソート可能 | framework7/components/sortable |
スワイプアウト | framework7/components/swipeout |
アコーディオン | framework7/components/accordion |
連絡先リスト | framework7/components/contacts-list |
バーチャルリスト | framework7/components/virtual-list |
リストインデックス | framework7/components/list-index |
タイムライン | framework7/components/timeline |
タブ | framework7/components/tabs |
パネル | framework7/components/panel |
カード | framework7/components/card |
チップ | framework7/components/chip |
フォーム | framework7/components/form |
インプット | framework7/components/input |
チェックボックス | framework7/components/checkbox |
ラジオ | framework7/components/radio |
トグル | framework7/components/toggle |
レンジ | framework7/components/range |
ステッパー | framework7/components/stepper |
スマートセレクト | framework7/components/smart-select |
グリッド | framework7/components/grid |
カレンダー | framework7/components/calendar |
ピッカー | framework7/components/picker |
無限スクロール | framework7/components/infinite-scroll |
プル・トゥ・リフレッシュ | framework7/components/pull-to-refresh |
データテーブル | framework7/components/data-table |
Fab | framework7/components/fab |
検索バー | framework7/components/searchbar |
メッセージ | framework7/components/messages |
メッセージバー | framework7/components/messagebar |
スワイパー | framework7/components/swiper |
フォトブラウザ | framework7/components/photo-browser |
通知 | framework7/components/notification |
オートコンプリート | framework7/components/autocomplete |
ツールチップ | framework7/components/tooltip |
ゲージ | framework7/components/gauge |
スケルトン | framework7/components/skeleton |
円グラフ | framework7/components/pie-chart |
エリアチャート | framework7/components/area-chart |
タイポグラフィー | framework7/components/typography |
テキストエディター | framework7/components/text-editor |
ブレッドクラム | framework7/components/breadcrumbs |