Angular PWA使用的Demo示例
什么是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倉(cāng)庫(kù)
上傳項(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是倉(cāng)庫(kù)名稱
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)文章
AngularJS表格樣式簡(jiǎn)單設(shè)置方法示例
這篇文章主要介紹了AngularJS表格樣式簡(jiǎn)單設(shè)置方法,結(jié)合實(shí)例形式分析了AngularJS結(jié)合bootstrap針對(duì)表格樣式的相關(guān)設(shè)置技巧,需要的朋友可以參考下2017-03-03AngularJS中的Directive自定義一個(gè)表格
本篇文章給大家介紹在angularjs中自定義一個(gè)有關(guān)表格的directive,涉及到angularjs directive相關(guān)知識(shí),對(duì)本文感興趣的朋友一起學(xué)習(xí)吧2016-01-01Angular.js之作用域scope''@'',''='',''&''實(shí)例詳解
這篇文章主要介紹了Angular.js之作用域scope'@','=','&'實(shí)例詳解,需要的朋友可以參考下2017-02-02angular1.x ui-route傳參的三種寫法小結(jié)
今天小編就為大家分享一篇angular1.x ui-route傳參的三種寫法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08總結(jié)AngularJS開發(fā)者最常犯的十個(gè)錯(cuò)誤
AngularJS是如今最受歡迎的JS框架之一,簡(jiǎn)化開發(fā)過程是它的目標(biāo)之一,這使得它非常適合于元型較小的apps的開發(fā),但也擴(kuò)展到具有全部特征的客戶端應(yīng)用的開發(fā)。下面給大家總結(jié)了AngularJS開發(fā)者最常犯的十個(gè)錯(cuò)誤,有需要的可以參考學(xué)習(xí)下。2016-08-08基于datepicker定義自己的angular時(shí)間組件的示例
這篇文章主要介紹了基于datepicker定義自己的angular時(shí)間組件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03angular-ngSanitize模塊-$sanitize服務(wù)詳解
本篇文章主要介紹了angular-ngSanitize模塊-$sanitize服務(wù)詳解 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法
今天小編就為大家分享一篇Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09