简体   繁体   English

Typescript:“ Compositions”类型上不存在该属性

[英]Typescript: property does not exist on type 'Compositions'

I have the following TypeScript file in my Angular2 project: 我的Angular2项目中有以下TypeScript文件:

import {Component} from '@angular/core';
import {CompositionsService} from './compositions.service';

@Component({
 selector: 'compositions',
 template: './compositions.html',
 styles: ['./compositions.css'],
 providers: [CompositionsService]
})

export class Compositions {

 private id: any;
 private instrumentation: string = '';

 constructor(private compositionsService: CompositionsService) { } 

 ngOnInit() {
}

  getCompositions() {
this.CompositionsService.byInstrument(this.id, this.instrumentation)
                 .subscribe(compositions =>    console.log(compositions.json()));
  }
}

This throws the following error: 这将引发以下错误:

Property 'CompositionsService' does not exist on type 'Compositions'.

I am setting the CompositionsService as a property of the Compositions in this file: 我在此文件中将CompositionsService设置为Compositions的属性:

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class CompositionsService {

  private url: string = 'http://localhost:3000/api';

  constructor(private http: Http) { }
  //TODO you don't actually need to pass an id here maybe just a page limit
  byInstrument(id: any, instrument: string): Observable<any> {
   return this.http.get(this.url + '/compositions?instrumentation=' + instrument);
  }
}

I am not sure how the property is not set on Compositions. 我不确定如何在合成中未设置该属性。

You named it compositionsService : 您将其命名为compositionsService

constructor(private compositionsService: CompositionsService) { }

But you call it CompositionsService : 但您称它为CompositionsService

this.CompositionsService.byInstrument(this.id, this.instrumentation)

Because javascript is case sensitive, the difference in the first character is causing that error, it should be: 由于javascript区分大小写,因此第一个字符的差异会导致该错误,因此应为:

this.compositionsService.byInstrument(this.id, this.instrumentation)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM