最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解使用路由延遲加載 Angular 模塊

 更新時間:2017年10月12日 08:22:42   作者:冠軍  
這篇文章主要介紹了詳解使用路由延遲加載 Angular 模塊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Angular 非常模塊化,模塊化的一個非常有用的特性就是模塊作為延遲加載點(diǎn)。延遲加載意味著可以在后臺加載一個模塊和其包含的所有組件等資源。這樣 Angular 就不需要在第一個界面從服務(wù)器下載所有的文件,直到您請求它,才下載相應(yīng)的模塊。這對提供性能和減少首屏的初始下載文件尺寸有巨大的幫助。而且它可以很容易設(shè)置。

這里將使用一個簡單示例來演示這個特性是如何工作的。將應(yīng)用拆分為多個不同的模塊,可以在需要的時候再進(jìn)行延遲加載。

延遲加載的路由需要在根模塊之外定義,所以,你需要將需要延遲加載的功能包含在功能模塊中。

我們使用 Angular CLI 來創(chuàng)建一個演示項(xiàng)目:Demo.

ng new demo

然后,進(jìn)入到 demo 文件夾中。安裝必要的 package。

npm i

在安裝之后,我們創(chuàng)建一個新的模塊 shop。在 angular CLI 中,ng 是命令提示指令,g 表示 generate,用來創(chuàng)建某類新 item。

創(chuàng)建新的名為 shop 的模塊就是:

ng g module shop

這會導(dǎo)致在 Angular 項(xiàng)目的 src/app 文件下創(chuàng)建一個新的文件夾,并添加一個名為 shop.module.ts 的模塊定義文件。

然后,我們在默認(rèn)的 app 模塊和新創(chuàng)建的 shop 模塊中分別創(chuàng)建組件。

ng g c home/home
ng g c shop/cart
ng g c shop/checkout 
ng g c shop/confirm

CLI 會將 home 分配到 app 模塊中,將 cart、checkout、confirm 分配到 shop 模塊中,比如,

此時的 shop.module.ts 內(nèi)容如下:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CheckoutComponent } from './checkout/checkout.component';
import { CartComponent } from './cart/cart.component';
import { ConfirmComponent } from './confirm/confirm.component';

@NgModule({
 imports: [
  CommonModule
 ],
 declarations: [CheckoutComponent, CartComponent, ConfirmComponent]
})
export class ShopModule { }

修改根組件

Angular CLI 默認(rèn)生成的 app.component.ts 組件是應(yīng)用的主頁面,其中包含了一些關(guān)于 Angular 的介紹信息,我們將它修改成我們需要的內(nèi)容。將默認(rèn)生成的 app.component.html 內(nèi)容修改為如下內(nèi)容。

<!--The content below is only a placeholder and can be replaced.-->
<h1>Lazy Load Module</h1>
<a [routerLink]="['/shop']" >Shop Cart</a>
<router-outlet>
</router-outlet>

這里提供了一個占位的 router-outlet,各個組件將顯示在這里面。

同時,提供了一個導(dǎo)航鏈接,可以直接導(dǎo)航到 /shop/cart 組件。

創(chuàng)建路由

根路由

首先創(chuàng)建根路由。

我們在 app 文件夾中,添加一個名為 main.routing.ts 的路由配置文件。內(nèi)容如下:

import { Routes } from '@angular/router';
// HomeComponent this components will be eager loaded
import { HomeComponent } from './home/home.component';

export const routes: Routes = [
  { path: '', component: HomeComponent, pathMatch: 'full' },
  { path: 'shop', loadChildren: './shop/shop.module#ShopModule' },
  { path: '**', component: HomeComponent }
];

其中,home 組件是正常的提前加載。

需要注意的是一下幾點(diǎn):

1. 我們使用了 loadChildren 來延遲加載一個模塊。而不是使用提前加載所使用的 component。
2. 我們使用了一個字符串而不是符號來避免提前加載。
3. 我們不僅定義了模塊的路徑,還提供了模塊的類名。

在 app.module.ts 中啟用根路由。主要需要使用 forRoot 來啟用根路由。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { routes } from './main.routing';
import { RouterModule } from '@angular/router';

@NgModule({
 declarations: [
  AppComponent,
  HomeComponent
 ],
 imports: [
  BrowserModule,
  RouterModule.forRoot(routes)
 ],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule { }

模塊路由

定義模塊路由

對于 shop 模塊來說,定義路由就沒有什么特別了,我們這里可以定義一個名為 shop.route.ts 的路由定義文件,內(nèi)容如下所示:

import { Routes } from '@angular/router'; 
import { CartComponent } from './cart/cart.component'; 
import { CheckoutComponent } from './checkout/checkout.component'; 
import { ConfirmComponent } from './confirm/confirm.component'; 
export const routes: Routes = [   
     { path: '', component: CartComponent },   
     { path: 'checkout', component: CheckoutComponent },  
     { path: 'confirm', component: ConfirmComponent } 
];

還需要修改一下模塊定義文件 shop.module.ts 文件,以使用這個路由定義。注意我們需要使用 forChild 來啟用子路由。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CheckoutComponent } from './checkout/checkout.component';
import { CartComponent } from './cart/cart.component';
import { ConfirmComponent } from './confirm/confirm.component';

import { routes } from './shop.routing'; 
import { RouterModule } from '@angular/router';

@NgModule({
 imports: [
  CommonModule,
  RouterModule.forChild(routes)
 ],
 declarations: [CheckoutComponent, CartComponent, ConfirmComponent]
})
export class ShopModule { }

已經(jīng)一切就緒了。

測試延遲加載

現(xiàn)在啟動應(yīng)用。

ng serve

默認(rèn)會在 4200 端口啟動應(yīng)用,請打開瀏覽器,訪問:http://localhost:4200/

訪問首頁的網(wǎng)絡(luò)訪問如下,其中并不包含功能模塊的內(nèi)容。

我們先將網(wǎng)絡(luò)請求的歷史記錄清除掉。

然后點(diǎn)擊鏈接,訪問 /shop/cart 的時候,網(wǎng)絡(luò)請求如下,可以看到一個新的腳本文件被加載,這里包含的就是延遲加載的功能模塊。

僅僅功能模塊被加載了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • AngularJS服務(wù)service用法總結(jié)

    AngularJS服務(wù)service用法總結(jié)

    這篇文章主要介紹了AngularJS服務(wù)service用法,結(jié)合實(shí)例形式總結(jié)分析了服務(wù)Service的概念、功能及自定義服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下
    2016-12-12
  • Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全的問題解決

    Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全的問題解決

    這篇文章主要給大家介紹了關(guān)于Angular4實(shí)現(xiàn)圖片上傳預(yù)覽路徑不安全問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • 使用Angular CLI從藍(lán)本生成代碼詳解

    使用Angular CLI從藍(lán)本生成代碼詳解

    這篇文章主要介紹了使用Angular CLI從藍(lán)本生成代碼詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • 通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例

    通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例

    本篇文章主要介紹了通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • AngularJS入門教程二:在路由中傳遞參數(shù)的方法分析

    AngularJS入門教程二:在路由中傳遞參數(shù)的方法分析

    這篇文章主要介紹了AngularJS在路由中傳遞參數(shù)的方法,結(jié)合實(shí)例形式分析了AngularJS實(shí)現(xiàn)路由中傳遞參數(shù)的相關(guān)技巧,并總結(jié)了相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下
    2017-05-05
  • 詳解angular2封裝material2對話框組件

    詳解angular2封裝material2對話框組件

    本篇文章主要介紹了詳解angular2封裝material2對話框組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Angular 多模塊項(xiàng)目構(gòu)建過程

    Angular 多模塊項(xiàng)目構(gòu)建過程

    這篇文章主要介紹了Angular 多模塊項(xiàng)目構(gòu)建過程,本文大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 基于angularJS的表單驗(yàn)證指令介紹

    基于angularJS的表單驗(yàn)證指令介紹

    下面小編就為大家?guī)硪黄赼ngularJS的表單驗(yàn)證指令介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • AngularJS 2.0新特性有哪些

    AngularJS 2.0新特性有哪些

    本文介紹了其在AtScript、改善依賴入駐、Annotations、路由方案等方面的改變。雖然不知道打破原有是否是件好事,不過由于不向后兼容,我們需要為遷移做好計(jì)劃
    2016-02-02
  • AngularJS 驗(yàn)證碼60秒倒計(jì)時功能的實(shí)現(xiàn)

    AngularJS 驗(yàn)證碼60秒倒計(jì)時功能的實(shí)現(xiàn)

    最近在做AngularJS 項(xiàng)目,這是寫的一個60秒倒計(jì)時功能,下面小編給大家介紹AngularJS 驗(yàn)證碼60秒倒計(jì)時功能的實(shí)現(xiàn),需要的朋友參考下吧
    2017-06-06

最新評論

伽师县| 汝南县| 洛扎县| 天长市| 琼结县| 定安县| 尼勒克县| 秀山| 原阳县| 凤庆县| 津市市| 滁州市| 邯郸市| 永平县| 无为县| 时尚| 卓资县| 渭南市| 德清县| 阜康市| 石台县| 江华| 武定县| 石阡县| 沁水县| 阿合奇县| 景洪市| 永嘉县| 商洛市| 翁牛特旗| 永德县| 大兴区| 德化县| 茂名市| 正宁县| 治县。| 泾阳县| 墨江| 濮阳县| 六枝特区| 读书|