Serve from Disk
在磁盤上運(yùn)行服務(wù)
The CLI supports running a live browser reload experience to users by running ng serve. This will compile the application upon file saves and reload the browser with the newly compiled application. This is done by hosting the application in memory and serving it via webpack-dev-server.
CLI支持通過運(yùn)行 ng serve 來為用戶運(yùn)行實時瀏覽器重新加載體驗。這將在文件保存時編譯應(yīng)用程序,并用新編譯的應(yīng)用程序重新載入瀏覽器。這是通過將應(yīng)用程序托管在內(nèi)存中并通過webpack-dev-server.來完成的。
If you wish to get a similar experience with the application output to disk please use the steps below. This practice will allow you to ensure that serving the contents of your dist dir will be closer to how your application will behave when it is deployed.
如果您希望獲得與應(yīng)用程序輸出到磁盤相似的體驗,請使用以下步驟。這種做法將允許您確保提供遠(yuǎn)程目錄的內(nèi)容將更接近您的應(yīng)用程序在部署時的行為方式。
Environment Setup
環(huán)境設(shè)置
Install a web server
安裝一個web服務(wù)器
You will not be using webpack-dev-server, so you will need to install a web server for the browser to request the application. There are many to choose from but a good one to try is lite-server as it will auto-reload your browser when new files are output.
您將不會使用webpack-dev-server,因此您需要安裝Web服務(wù)器以供瀏覽器請求應(yīng)用程序。有很多可供選擇的,但最好嘗試的是lite-server,因為它會在輸出新文件時自動重新加載瀏覽器
Usage
使用
You will need two terminals to get the live-reload experience. The first will run the build in a watch mode to compile the application to the dist folder. The second will run the web server against the dist folder. The combination of these two processes will mimic the same behavior of ng serve.
您將需要兩個終端才能獲得實時加載體驗。第一個將以監(jiān)視模式運(yùn)行構(gòu)建以將應(yīng)用程序編譯到 dist 文件夾。第二個將針對 dist 文件夾運(yùn)行Web服務(wù)器。這兩個過程的結(jié)合將模擬ng服務(wù)的相同行為。
1st terminal - Start the build
第一個終端,開始構(gòu)建
ng build --watch
2nd terminal - Start the web server
第二個終端。開始web服務(wù)器
lite-server --baseDir="dist"
When using lite-server the default browser will open to the appropriate URL.
使用 lite-server 時,默認(rèn)瀏覽器將打開相應(yīng)的URL。