@swkng/httpmock
v15.0.1125
Published
此版為 [Angular CLI](https://github.com/angular/angular-cli) version 15.0.0 的 Http Mock
Downloads
4
Readme
HttpMock
此版為 Angular CLI version 15.0.0 的 Http Mock
如何使用
宣告 Mock Controller
mock.controller.ts
import { SwkHttpMockRequest } from '@swk'; class MockApiController { static GetInfo() { return { name: 'lawrence', tel: '0900000000', exp: new Date().getTime() }; } } export const MockApiRouter = { 'GET /api/Info': (req: SwkHttpMockRequest) => MockApiController.GetInfo() };
在
app.module.ts
中 import// Http Mock import { SwkHttpMockModule } from '@swk'; import * as MOCKDATA from './mock.controller'; import { HttpClientModule } from '@angular/common/http'; const MOCKMODULES = []; MOCKMODULES.push(SwkHttpMockModule.forRoot({ data: MOCKDATA, log: true, delay: 300 })); @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, //<-- 加入此項目 ...MOCKMODULES //<-- 加入此項目 ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Http 呼叫,使用原本方法即可,無須改變任何程式
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.less'] }) export class AppComponent { constructor(private http: HttpClient) { } onHttpMock() { this.http.get('/api/Info').subscribe(result => this.mockResult = result); } }