0
我想寫空方法「基地」的服務,應該由其他開發人員編寫的「適配器」針對不同的後端系統 擴展Angular2 +服務
這裏覆蓋是我的「基地」認證服務:
import { Injectable } from '@angular/core';
import { Http} from '@angular/http';
import {constants} from '../../constants/constants';
@Injectable()
export class ApiAuthServiceProvider {
constructor(public http: Http) {
console.log('Hello Base class ApiAuthServiceProvider Provider');
}
getVersion(credentials): Promise <any> {
throw constants.NOT_IMPLEMENTED;
}
getToken (credentials): Promise <any> {
throw constants.NOT_IMPLEMENTED;
}
getCredentials(): any {
// do some stuff which is generic
// and will be part of the base impleementation
}
}
這是我提出的適配器,對於一個特定的後端
import { Injectable } from '@angular/core';
import { Http , URLSearchParams} from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import {constants} from '../../../constants/constants';
import {ApiAuthServiceProvider} from '../../../providers/api-auth-service/api-auth-service';
@Injectable()
export class zmApiAuthServiceProvider extends ApiAuthServiceProvider {
constructor(public http: Http) {
super(http);
console.log('Hello ZMApiAuthServiceProvider Provider');
}
getVersion(credentials): Promise <any> {
return this.http.get (credentials.url+'/api/host/getVersion.json')
.toPromise()
}
getToken(credentials): Promise <any> {
// my stuff
}
}
擴展這項服務
這裏是我如何app.component.ts
import {ApiAuthServiceProvider} from '../providers/api-auth-service/api-auth-service'
import {constants} from '../constants/constants';
// Classes for adapters in use
import {zmApiAuthServiceProvider} from '../adapters/zoneminder/providers/zm-api-auth-service';
@Component({
templateUrl: 'app.html',
providers: [
{provide: ApiAuthServiceProvider, useClass: zmApiAuthServiceProvider}]
})
關聯的適配器服務最後,調用在app.components.ts
constructor(public auth: ApiAuthServiceProvider) {
this.auth.getToken(credentials)
}
試圖運行此產生一大堆錯誤的構造:
- 合併聲明'ApiAuthServiceProvider'中的單個聲明必須全部導出或全部爲本地。
- 導入聲明與'ApiAuthServiceProvider'的本地聲明衝突。
- 模塊'「xxx/src/adapters/zm/providers/zm-api-auth-service''沒有導出成員'zmApiAuthServiceProvider'。
我接近這個錯誤嗎?謝謝。
您的ApiAuthServiceProvider類不應該抽象嗎?我的意思是你在任何地方使用它? – n00dl3
正確,應該是。但它並不能解決我的錯誤。 – user1361529
看看[注入令牌](https://angular.io/guide/dependency-injection#dependency-injection-tokens) – n00dl3