0

I am new to angular and I was referring standard angular material present on angular site for the tutorial and I come upon part "Get Data from server". In this, I have created my in-memory dataservice like this.

import { Injectable } from '@angular/core';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Hero } from './hero';
    
@Injectable({
  providedIn: 'root',
})
export class InMemoryDataService implements InMemoryDbService {
  createDb() {
    const data = [
      { id: 11, name: 'Dr Nice' },
      { id: 12, name: 'Narco' },
      { id: 13, name: 'Bombasto' },
      { id: 14, name: 'Celeritas' },
      { id: 15, name: 'Magneta' },
      { id: 16, name: 'RubberMan' },
      { id: 17, name: 'Dynama' },
      { id: 18, name: 'Dr IQ' },
      { id: 19, name: 'Magma' },
      { id: 20, name: 'Tornado' }
    ];

    return {data};
  }
}

And call it in my service class

import { Injectable } from '@angular/core';
import {Hero} from './hero';
import {HEROES} from './mock-heroes';
import { Observable, of } from 'rxjs';
import {MessageService} from './message.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map, tap } from 'rxjs/operators';
        
@Injectable({
  providedIn: 'root'
})
export class HeroService {
  constructor( 
    private http: HttpClient,
    private messageService: MessageService
  ) { }
        
  private heroesUrl = 'api/data'; 
  httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  };
       
  getHeroes(): Observable<Hero[]> {
    return this.http.get<Hero[]>(this.heroesUrl)
      .pipe(
        tap(_ => this.log('fetched Heroes')),
        catchError(this.handleError<Hero[]>('getHeroes', []))
    );
  }
}

so my question is can we change the base of web URI from api/data to test/data or demo/data something like that?

4

1 回答 1

1

是的你可以。apiBase您可以配置一个属性。在我的测试应用程序中,我想发送请求/fake-api而不是仅仅发送请求api,这段代码有帮助:

HttpClientInMemoryWebApiModule.forRoot(
  InMemoryDataService, { dataEncapsulation: false, apiBase: 'fake-api/' }
)
于 2020-12-23T08:14:14.337 回答