這篇文章主要介紹了Angular5/Angular6項(xiàng)目添加熱更新(HMR)功能的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有船營免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
Angular5/Angular6項(xiàng)目如何添加熱更新(HMR)功能,具體如下:
A:什么是HMR?
Q:HMR(熱替換)用于在運(yùn)行的應(yīng)用程序中更新代碼而不需要重建它。這將導(dǎo)致更快的更新和更少的全頁重新加載。
angular6-hmr
提供angular6以上HMR(熱更新)功能
步驟
1、進(jìn)入angular項(xiàng)目父級目錄內(nèi)
git clone https://github.com/staven630/angular6-hmr
angular6-hmr目錄與angular項(xiàng)目(例如:my-app)是同級關(guān)系
2、執(zhí)行g(shù)ulp hmr --dir angular目錄名
如:
npm i gulp hmr --dir my-app
3、進(jìn)入angular項(xiàng)目目錄,安裝@angularclass/hmr
npm install --save-dev @angularclass/hmr --registry https://registry.npm.taobao.org
4、這樣angular項(xiàng)目的HMR就配置完成了,執(zhí)行
npm run hmr
注:保持項(xiàng)目名(package.json中的name)與項(xiàng)目目錄名一致
以下為手動配置步驟
Angular6添加HMR
environments目錄
environments.ts和environment.prod.ts增加hmr: false
export const environment = { hmr: false };
復(fù)制environment新增environment.hmr.ts修改hmr:true
export const environment = { hmr: true };
.angular.json文件
build的configurations中添加
"hmr": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.hmr.ts" } ] }
serve的configurations中添加
"hmr": { "hmr": true, "browserTarget": "my-app:build:hmr" }
tsconfig.app.json的compilerOptions的types中添加
"types": ["node"]
package.json的scripts中添加
"hmr": "ng serve --configuration hmr --open"
安裝依賴
npm install --save-dev @angularclass/hmr
src目錄下創(chuàng)建hmr.ts
import { NgModuleRef, ApplicationRef } from '@angular/core'; import { createNewHosts } from '@angularclass/hmr'; export const hmrBootstrap = (module: any, bootstrap: () => Promise>) => { let ngModule: NgModuleRef ; module.hot.accept(); bootstrap().then(mod => ngModule = mod); module.hot.dispose(() => { const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef); const elements = appRef.components.map(c => c.location.nativeElement); const makeVisible = createNewHosts(elements); ngModule.destroy(); makeVisible(); }); };
修改main.ts
import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; import { hmrBootstrap } from './hmr'; if (environment.production) { enableProdMode(); } const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule); if (environment.hmr) { if (module[ 'hot' ]) { hmrBootstrap(module, bootstrap); } else { console.error('HMR is not enabled for webpack-dev-server!'); console.log('Are you using the --hmr flag for ng serve?'); } } else { bootstrap().catch(err => console.log(err)); }
Angular5添加HMR
environments目錄
environments.ts和environment.prod.ts增加hmr: false
export const environment = { hmr: false };
復(fù)制environment新增environment.hmr.ts修改hmr:true
export const environment = { hmr: true };
.angular-cli.json的environments中添加
"hmr": "environments/environment.hmr.ts"
在package.json的scripts中增加
"hmr": "ng serve --hmr -e=hmr --open"
安裝依賴
npm install --save-dev @angularclass/hmr
src目錄下創(chuàng)建hmr.ts
import { NgModuleRef, ApplicationRef } from '@angular/core'; import { createNewHosts } from '@angularclass/hmr'; export const hmrBootstrap = (module: any, bootstrap: () => Promise>) => { let ngModule: NgModuleRef ; module.hot.accept(); bootstrap().then(mod => ngModule = mod); module.hot.dispose(() => { const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef); const elements = appRef.components.map(c => c.location.nativeElement); const makeVisible = createNewHosts(elements); ngModule.destroy(); makeVisible(); }); };
修改main.ts
import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; import { hmrBootstrap } from './hmr'; if (environment.production) { enableProdMode(); } const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule); if (environment.hmr) { if (module[ 'hot' ]) { hmrBootstrap(module, bootstrap); } else { console.error('HMR is not enabled for webpack-dev-server!'); console.log('Are you using the --hmr flag for ng serve?'); } } else { bootstrap().catch(err => console.log(err)); }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Angular5/Angular6項(xiàng)目添加熱更新(HMR)功能的方法”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!