国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

基于angular6.0實現的一個組件懶加載功能示例

2024-05-06 15:19:59
字體:
來源:轉載
供稿:網友

我們常常會遇到這樣一個問題,當我們使用一個第三方控件庫的時候,我們只用到了其中 1 個或某幾個組件,會連帶一大堆無用的東西,造成體積臃腫不堪。又或者首頁用到的組件較多,首頁加載速度緩慢,這個時候,我們或許需要加載用戶可視范圍內用到的組件,隨著用戶的瀏覽下拉,我們再去加載這些組件,漸進式加載,漸進式體驗,這個時候你或許就用到了本工具所實現的功能。或者一個頁面的某些不重要區域,比如第三方廣告又或者不重要的元素,可以采用懶加載懶渲染,降低用戶首屏等待時間。一切都在用戶不知不覺中進行。大大增加用戶體驗,特別是中大型項目,優化必備!

項目地址github

安裝

yarn add iwe7-lazy-load

使用

import { Iwe7LazyLoadModule, LazyComponentsInterface } from 'iwe7-lazy-load';// 用到的懶加載組件let lazyComponentsModule: LazyComponentsInterface[] = [ { // 組件的selector path: 'lazy-test', // 組件的相對地址 loadChildren: './lazy-test/lazy-test.module#LazyTestModule' }];@NgModule({ imports: [Iwe7LazyLoadModule.forRoot(lazyComponentsModule)], // 注意加上這些 schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]})export class AppModule {}
<div #ele> <lazy-test></lazy-test></div>
import { LazyLoaderService } from 'iwe7-lazy-load';@ViewChild('ele') ele: ElementRef;constructor( public lazyLoader: LazyLoaderService, public view: ViewContainerRef) {}ngOnInit() { // 開始渲染懶組件 this.lazyLoader.init(this.ele.nativeElement, this.view);}

定義懶加載組件 demo

import { LazyComponentModuleBase } from 'iwe7-lazy-load';@Component({ selector: 'lazy-test', template: ` i am a lazy`})export class LazyTestComponent {}@NgModule({ imports: [ RouterModule.forChild([{  path: '',  component: LazyTestComponent }]) ], declarations: [LazyTestComponent]})export class LazyTestModule extends LazyComponentModuleBase { getComponentByName(key: string): Type<any> { return LazyTestComponent; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 化州市| 合水县| 新丰县| 澄江县| 禄丰县| 神农架林区| 临泽县| 连山| 仁怀市| 绥芬河市| 洛浦县| 宜昌市| 晋州市| 锡林郭勒盟| 曲阳县| 吉安市| 永定县| 上栗县| 临猗县| 安康市| 江门市| 中宁县| 扬中市| 游戏| 长岭县| 息烽县| 兴和县| 化州市| 曲水县| 宝兴县| 乐都县| 敖汉旗| 神木县| 平乐县| 平远县| 无为县| 黔南| 阿拉善右旗| 周口市| 涪陵区| 青浦区|