2017-09-12 120 views
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'。

我接近这个错误吗?谢谢。

+0

您的ApiAuthServiceProvider类不应该抽象吗?我的意思是你在任何地方使用它? – n00dl3

+0

正确,应该是。但它并不能解决我的错误。 – user1361529

+0

看看[注入令牌](https://angular.io/guide/dependency-injection#dependency-injection-tokens) – n00dl3

回答

0

好的,我不知道为什么当我发布时这不起作用 - 这可能是我没有保存的东西!

这种方法效果非常好 - 它可能是某个)魔法B)我的疏忽保存

我会用魔法去。