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

Angular PWA使用的Demo示例

 更新時(shí)間:2019年01月31日 11:05:01   作者:FlyWine  
這篇文章主要介紹了Angular PWA使用的Demo示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

什么是PWA

PWA(Progressive Web App)利用TLS,webapp manifests和service workers使應(yīng)用程序能夠安裝并離線使用。 換句話說,PWA就像手機(jī)上的原生應(yīng)用程序,但它是使用諸如HTML5,JavaScript和CSS3之類的網(wǎng)絡(luò)技術(shù)構(gòu)建的。 如果構(gòu)建正確,PWA與原生應(yīng)用程序無法區(qū)分。

PWA 的主要特點(diǎn)包括下面三點(diǎn):

  • 可靠 - 即使在不穩(wěn)定的網(wǎng)絡(luò)環(huán)境下,也能瞬間加載并展現(xiàn)
  • 體驗(yàn) - 快速響應(yīng),并且有平滑的動(dòng)畫響應(yīng)用戶的操作
  • 粘性 - 像設(shè)備上的原生應(yīng)用,具有沉浸式的用戶體驗(yàn),用戶可以添加到桌面

PWA 本身強(qiáng)調(diào)漸進(jìn)式,并不要求一次性達(dá)到安全、性能和體驗(yàn)上的所有要求,開發(fā)者可以通過 PWA Checklist 查看現(xiàn)有的特征。

創(chuàng)建項(xiàng)目

Step 1.創(chuàng)建項(xiàng)目

我們使用Angular CLI來創(chuàng)建PWA程序,首先我們安裝。

npm install -g @angular/cli
npm install -g @angular/service-worker

首先我們需要確定angular/cli版本在1.6.0或以上。

最新版本6.0.0將無效,應(yīng)該后續(xù)會(huì)修復(fù)。

.angular-cli.json文件被更名為angular.json

如果是全新項(xiàng)目

可以使用angular/cli幫你創(chuàng)建一個(gè)Angular Service Worker項(xiàng)目:

ng new PWCat --service-worker

就這樣,cli會(huì)幫你安裝@angular/service-worker,在.angular-cli.json中啟用serviceWorker,為app注冊(cè)serviceWorker和生成默認(rèn)配置的ngsw-config.json

生成的文件中,注意檢查一下app.module,ts,其中serviceWorkerModule注冊(cè)的時(shí)候應(yīng)該是這樣:

imports: [
  // other modules...
  ServiceWorkerModule.register('/ngsw-worker.js', {enabled: environment.production})
 ],

在Angular 6.0.0中ng new PWCat --service-worker已經(jīng)被廢棄,使用下面的命令為項(xiàng)目添加pwa

ng add @angular/pwa

執(zhí)行后的結(jié)果

CREATE ngsw-config.json (392 bytes)
UPDATE angular.json (3464 bytes)
UPDATE package.json (1446 bytes)
UPDATE src/app/app.module.ts (851 bytes)

如果是已有項(xiàng)目

對(duì)于老版本,也就是Angular 6.0.0以前:

安裝@angular/service-worker:

npm install @angular/service-worker --save

在項(xiàng)目目錄下面新增ngsw-config.json文件:

// src/ngsw-config.json
{
 "index": "/index.html",
 "assetGroups": [{
  "name": "app",
  "installMode": "prefetch",
  "resources": {
   "files": [
    "/favicon.ico",
    "/index.html"
   ],
   "versionedFiles": [
    "/*.bundle.css",
    "/*.bundle.js",
    "/*.chunk.js"
   ]
  }
 }, {
  "name": "assets",
  "installMode": "lazy",
  "updateMode": "prefetch",
  "resources": {
   "files": [
    "/assets/**"
   ]
  }
 }]
}

在.angular-cli.json中啟用service worker:

// .angular-cli.json
...
{ 
 "apps": [{ 
  ..., 
  "serviceWorker": true
 }]
}

然后在app.module.ts中注冊(cè)Service Worker :

// src/app.module.ts
...
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
@NgModule({
 ...
 imports: [
  ... ,
  ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production })
 ],
})
...

這樣項(xiàng)目中就引入Service Worker。

對(duì)于新版本6.0.0

使用下面命令創(chuàng)建。

ng add @angular/pwa

將會(huì)創(chuàng)建:
- manifest
- service worker

Step 2. 添加Angular Material模塊

安裝material和cdk

npm install --save @angular/material @angular/cdk

安裝主題

/* src/styles.css */
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

安裝normalize.css,作用是優(yōu)化html樣式

npm install --save normalize.css

然后在 styles.css中添加:

/* src/styles.css */
@import '~normalize.css/normalize.css';
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

添加Material Module

// src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MatToolbarModule } from '@angular/material';
import { AppComponent } from './app.component';
@NgModule({
 declarations: [ AppComponent ],
 imports: [
  BrowserModule,
  MatToolbarModule
 ],
 providers: [],
 bootstrap: [ AppComponent ]
})
export class AppModule { }

修改app.component.ts和app.component.html

// src/app/app.component.ts
...
export class AppComponent {
 title = 'Progressive Web Cat';
}
<!-- src/app/app.component.html -->
<mat-toolbar color="primary">
 {{ title }}
</mat-toolbar>

Step 3.創(chuàng)建一個(gè)圖片模塊

生成模塊

ng generate component img-card

將其添加到app.module.ts

// src/app/app.module.ts
...
import { AppComponent } from './app.component';
import { ImgCardComponent } from './img-card/img-card.component';
@NgModule({
 declarations: [
  AppComponent,
  ImgCardComponent
 ],
 ...

將img-card模塊添加到app.component.html中:

<!-- src/app/app.component.html -->
<mat-toolbar color="primary">
 {{title}}
</mat-toolbar>
<app-img-card></app-img-card>

修改app.module.ts

...
@NgModule({
...
 imports: [
  BrowserModule,
  MatToolbarModule,
  MatCardModule,
  MatButtonModule
 ],
...
})

修改img-card.component.ts

添加一個(gè)全局的CatImage類

// src/app/img-card/img-card.component.ts
...
class CatImage {
 message: string;
 api: string;
 fontsize: number;
}
...

修改ImgCardComponent

// src/app/img-card/img-card.component.ts
...
export class ImgCardComponent implements OnInit {

 private image: CatImage = {
  message: 'Progressive Web Cat',
  api: 'https://cataas.com/cat/says/',
  fontsize: 40
 };
 public src: string;
 ngOnInit() {
  this.generateSrc();
 }
 generateSrc(): void {
  this.src = this.image.api + this.image.message + 
    '?size=' + this.image.fontsize +
    '&ts=' + Date.now();
 }
...

修改img-card.component.html

// src/app/img-card/img-card.component.html
<mat-card>
 <mat-card-actions>
  <button 
   color="primary" 
   (click)="generateSrc()"
   mat-button 
   mat-raised-button>
   Give me another cat
  </button>
 </mat-card-actions>
 <img 
  src="{{ src }}" 
  alt="Cute cat"
  mat-card-image>
</mat-card>

修改img-card.component.css

// src/app/img-card/img-card.component.css
.mat-card {
 width: 400px;
 margin: 2rem auto;
}
.mat-card .mat-card-actions {
 padding-top: 0;
}
.mat-card .mat-button {
 margin: 0 auto;
 display: block;
}

Step 4.離線狀態(tài)

修改ImgCardComponent

...
disabled = false;
ngOnInit() {

  if (navigator.onLine) {
   this.generateSrc();
  } else {
   this.disabled = true;
   this.src = 'assets/offline.jepg';
  }
}
...

修改`img-card.component.html

<mat-card>
 <mat-card-actions>
  <button 
   color="primary" 
   (click)="generateSrc()"
   mat-button
   disabled="disabled"
   mat-raised-button>
   Give me another cat
  </button>
 </mat-card-actions>
 <img 
  src="{{ src }}" 
  alt="Cute cat"
  mat-card-image>
</mat-card>

然后構(gòu)建部署:

ng build --prod

部署

由于https的限制,我們暫時(shí)部署到github上。

創(chuàng)建Github倉庫

上傳項(xiàng)目

git add .
git commit -m "Upload project to github"
git remote add origin git@github.com:{username}/{repo name}.git
git push --set-upstream origin master

編譯

PWCat是倉庫名稱

ng build --prod --base-href "/PWCat/"

新建github pages分支

git checkout -b "gh-pages"
git push --set-upstream origin gh-pages
git checkout master

部署到github

npm i -g angular-cli-ghpages
ngh "編譯的文件夾"

然后在github項(xiàng)目的settings里面GitHub Pages選項(xiàng)里設(shè)置GitHub Pages 分支為gh-pages

此時(shí)就可以使用網(wǎng)址https://93alliance.github.io/PWCat/訪問了。

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

相關(guān)文章

  • 深入理解Angular4訂閱(Subscribe)與取消

    深入理解Angular4訂閱(Subscribe)與取消

    這篇文章主要介紹了深入理解Angular4訂閱(Subscribe)與取消,詳細(xì)的介紹了訂閱與取消的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 詳談Angular路由與Nodejs路由的區(qū)別

    詳談Angular路由與Nodejs路由的區(qū)別

    下面小編就為大家?guī)硪黄斦凙ngular路由與Nodejs路由的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • 詳解Angular動(dòng)態(tài)組件

    詳解Angular動(dòng)態(tài)組件

    本文主要介紹了Angular動(dòng)態(tài)組件,對(duì)此感興趣的同學(xué),可以親自實(shí)驗(yàn)一下。
    2021-05-05
  • 深入探究AngularJs之$scope對(duì)象(作用域)

    深入探究AngularJs之$scope對(duì)象(作用域)

    本篇文章主要介紹了深入探究AngularJs之$scope對(duì)象(作用域),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Angular4學(xué)習(xí)之Angular CLI的安裝與使用教程

    Angular4學(xué)習(xí)之Angular CLI的安裝與使用教程

    網(wǎng)上很多教程過時(shí),命令在angular4中不適用等等,所以下面這篇文章主要給大家介紹了關(guān)于Angular4學(xué)習(xí)之Angular CLI的安裝與使用教程的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • AngularJS 過濾器的簡(jiǎn)單實(shí)例

    AngularJS 過濾器的簡(jiǎn)單實(shí)例

    本文主要介紹AngularJS 過濾器,這里提供詳細(xì)了AngularJS 過濾器詳細(xì)資料,并提供簡(jiǎn)單實(shí)例,有需要的朋友可以參考下
    2016-07-07
  • AngularJs Dependency Injection(DI,依賴注入)

    AngularJs Dependency Injection(DI,依賴注入)

    本文主要介紹AngularJs Dependency Injection,這里整理了詳細(xì)資料及示例代碼有興趣的小伙伴可以參考下
    2016-09-09
  • angular 實(shí)現(xiàn)同步驗(yàn)證器跨字段驗(yàn)證的方法

    angular 實(shí)現(xiàn)同步驗(yàn)證器跨字段驗(yàn)證的方法

    幾乎每個(gè)web應(yīng)用都會(huì)用到表單,那么驗(yàn)證器就是必不可少的東西,這篇文章主要介紹了angular 實(shí)現(xiàn)同步驗(yàn)證器跨字段驗(yàn)證的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • AngularJS入門教程之路由與多視圖詳解

    AngularJS入門教程之路由與多視圖詳解

    本文主要介紹AngularJS 路由與多視圖詳解,這里整理了相關(guān)資料及示例代碼,詳細(xì)說明了在開發(fā)過程中如何應(yīng)用,有興趣的朋友可以看下
    2016-08-08
  • 詳解Angular路由之路由守衛(wèi)

    詳解Angular路由之路由守衛(wèi)

    這篇文章主要介紹了詳解Angular路由之路由守衛(wèi),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05

最新評(píng)論

射洪县| 祁东县| 益阳市| 修文县| 永丰县| 德钦县| 平定县| 赤水市| 弥勒县| 自贡市| 文昌市| 宣化县| 扎鲁特旗| 丹寨县| 彭阳县| 介休市| 耒阳市| 玛纳斯县| 佛教| 昌邑市| 永兴县| 莲花县| 河东区| 普格县| 贵溪市| 读书| 毕节市| 绍兴市| 西乌| 廊坊市| 南和县| 临高县| 平谷区| 古丈县| 泗水县| 海安县| 金沙县| 任丘市| 灵台县| 洪雅县| 西乡县|