Previously we have 'OpaqueToken', but it is DEPRECATED. The new one is called 'InjectionToken'.

The difference between OpaqueToken is for InjectionToken we are able to pass the type, but OpaqueToken not.

 

// token.ts

import { InjectionToken } from '@angular/core';

export const API_TOKEN = new InjectionToken<string>('api');
//app.module.ts

  providers: [
    { provide: API_TOKEN, useValue: '/api/pizzas' }
  ]
// service.ts

import { API_TOKEN } from './token';

@Injectable()
export class FoodService {
  constructor(
    private http: Http,
    @Inject(API_TOKEN) private api: string
  ) {}
  getFood(): Observable<any[]> {
    return this.http.get(this.api)
      .map(response => response.json());
  }
}

 

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2021-06-14
  • 2022-02-24
  • 2021-09-07
  • 2021-10-19
  • 2022-12-23
  • 2021-07-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案