tôi tự hỏi nếu có một cách thích hợp để tiêm giao diện trong Angular2? (xem bên dưới)Có thể tiêm giao diện với angular2 không?
Tôi nghĩ rằng điều này có liên quan với trang trí bị thiếu @Injectable() trên giao diện, nhưng có vẻ như điều này không được phép.
Trân trọng.
Khi CoursesServiceInterface được thực hiện như một giao diện, trình biên dịch nguyên cảo phàn nàn "CoursesServiceInterface không thể tìm thấy cái tên":
import {CoursesServiceInterface} from './CoursesService.interface';
import {CoursesService} from './CoursesService.service';
import {CoursesServiceMock} from './CoursesServiceMock.service';
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
GlobalService,
provide(CoursesServiceInterface, { useClass: CoursesServiceMock })
]);
nhưng với CoursesServiceInterface như một giao diện:
import {Injectable} from 'angular2/core';
import {Course} from './Course.class';
//@Injectable()
export interface CoursesServiceInterface {
getAllCourses(): Promise<Course[]>;//{ return null; };
getCourse(id: number): Promise<Course>;// { return null; };
remove(id: number): Promise<{}>;// { return null; };
}
Khi dịch vụ là một lớp học, trình biên dịch TypeScript không phàn nàn nữa:
import {Injectable} from 'angular2/core';
import {Course} from './Course.class';
@Injectable()
export class CoursesServiceInterface {
getAllCourses() : Promise<Course[]> { return null; };
getCourse(id: number) :Promise<Course> { return null; };
remove (id: number) : Promise<{}> { return null; };
}
useClass: là những gì tôi đã mất tích ... quá xấu mà không có trong hướng dẫn chính thức – Aligned
https://angular.io/docs/ts/ mới nhất/cookbook/dependency-injection.html #! # usevalue –