現今電腦使用者處理資料大都是透過呼叫Http服務到伺服器執行相關的處理,但有時候你只想測試並不想真的要執行Http服務到遠端伺服器通訊工作。在Angular裡,它提供了一個套件可供使用者模擬終端伺服器通訊。該套件為記憶體Web Api(In-Memory Web Api)。安裝該套件後,Http服務會被該記憶體Web Api攔截和模擬響應。
要安裝該套件需要DOS到專案的目錄執行npm指令

執行完後需要在AppModule中匯入HttpClientModule和InMemoryWebApiModule模組

接者用forRoot()接受一個InMemoryDataService來初始化記憶體資料庫

以下為AppData ts檔,該class需要implements InMemoryDbService和建立createDb方法

Product類別

記得把AppData也加入到AppModule裡

透過rxjs執行 http get服務得到產品(Product)資訊

Html頁面

已上顯示如何用該套件(In-Memory Web Api)模擬資料的查詢。除了查詢,新增,刪除和更新也可用該套件模擬達成。