Angular 2注入服务到另一个服务是未定义的

时间:2017-04-07 07:28:28

标签: angular typescript dependency-injection

我正在尝试创建一个小框架,以便我更轻松地实现CRUD功能。

因此,我已经创建了这样的BaseHttpService:

... <imports> ...
@Injectable()
export class BaseHttpService {
  constructor(
    private http: Http
  ) {
    console.log('ctor');
    this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
  }
  public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
  ... <other methods> ...
}

然后我有我的BaseEntityReadOnlyService使用这个BaseHttpService来创建CRUD的读取功能:

@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
  constructor(
    @Inject(forwardRef(() => BaseHttpService)) public baseHttpService: BaseHttpService
    // public baseHttpService: BaseHttpService
  ) { 
    console.log('ro ctor');
    console.log(this.baseHttpService); // <-- this is always undefined
  }

  getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
    const url = `${this.baseUrl}`;
    console.log(this.baseHttpService);
    return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
  }
  ... <more methods>
}

在我的FrameworkModule中,我尝试了很多:

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    CommonModule,
    HttpModule
  ],
  providers: [
    BaseHttpService,
    // BaseAppSumEntityReadOnlyService
    { provide: BaseAppSumEntityReadOnlyService, useClass: BaseAppSumEntityReadOnlyService, deps: [ BaseHttpService ] },
  ]
})
export class AppSumFrameworkModule { }

这是我目前所拥有的,但这不起作用。我得到的错误是:

  

无法读取属性&#39; getPagedResultOfType&#39;未定义的

如何在BaseAppSumEntityReadOnlyService中注入BaseHttpService?

1 个答案:

答案 0 :(得分:1)

像这样导入您的服务:

import { BaseHttpService} from '../servicePath';

然后在构造函数中声明它:

constructor(
    private baseHttpService: BaseHttpService
  ) { 
    console.log(this.baseHttpService); 
  }

让我知道它是否像这样工作,如果没有,请复制/粘贴控制台错误。 :d

修改1:

服务:BaseHttpService

... <imports> ...
@Injectable()
export class BaseHttpService {
  constructor(
    private http: Http
  ) {
    console.log('ctor');
    this.apiUrl$.subscribe(apiUrl => this.rootApiUrl = apiUrl.removeTrailingSlash());
  }
  public getPagedResultOfType<T extends IBaseEntity>(url: string, vm: ISearchVm<T>, options?: RequestOptions): Observable<PagedResult<T>>{ ... <implementation> ... }
  ... <other methods> ...
}

服务:BaseAppSumEntityReadOnlyService

@Injectable()
export class BaseAppSumEntityReadOnlyService<T extends IBaseEntity>{
  constructor(
    private baseHttpService: BaseHttpService
  ) { 
    console.log('ro ctor');
    console.log(this.baseHttpService); // <-- this is always undefined
  }

  getAll(vm: ISearchVm<T>): Observable<PagedResult<T>>{
    const url = `${this.baseUrl}`;
    console.log(this.baseHttpService);
    return this.baseHttpService.getPagedResultOfType<T>(url, vm); // <-- this fails because baseHttpService is undefined: Cannot read property 'getPagedResultOfType' of undefined
  }
  ... <more methods>
}

<强>模块:

@NgModule({
  declarations: [
    myComponent
  ],
  imports: [
    CommonModule,
    HttpModule
  ],
  providers: [
    BaseHttpService,
    BaseAppSumEntityReadOnlyService,
  ]
})
export class AppSumFrameworkModule { }

组件:myComponent

... <imports> ...
@Component()
export class myComponent {
 constructor(
    private baseAppService: BaseAppSumEntityReadOnlyService
  ) { 
    console.log(this.baseAppService); 
  }
}