角度 2 组件注入服务:"[ts] Property doesn't exist on the type"



我在Angular 2中是菜鸟。我正在开发一个简单的应用程序。当我在Angular 2应用中使用服务时,我有问题。当我想在组件上使用它时,我无法在ngoninit()组件方法上使用getfavoritos()服务方法。

节点控制台显示此MESAGGE:

src/app/app/fairitos-list/fairitos-list.component.ts(30,31)中的错误: 错误ts2339:属性'getfavoritos'不存在类型'类型 fairitoservice'。

这是我的服务:

favoitos.service.ts

import { Settings } from './../settings/settings';
import { Favorito } from './../models/favorito';
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map' ;
import {Observable} from 'rxjs/Observable';
@Injectable()
export class FavoritosService {
 public url: string;
 constructor(private _http: Http) {
   this.url = Settings.GET_FAVORITOS;
 }
 getFavoritos() {
   return this._http.get(this.url)
      .map( res => res.json()) ;
 }
}

这是我的组成部分:

fairitos-list.component.ts

import { Favorito } from './../models/favorito';
import { FavoritosService } from './../service/favoritos.service'; 
import { Component, OnInit} from '@angular/core'; 
@Component({
   providers : [FavoritosService], 
   selector: 'app-favoritos-list',
   templateUrl: './favoritos-list.html',
   styleUrls: ['./favoritos.css']
})
export class FavoritosListComponent implements OnInit {
   public title: string;
   public errorMessage;

   constructor(       
      private _favoritoService = FavoritosService;        
   ) {
      this.title = 'Listado de marcadores:';
   }
   ngOnInit() {
      console.log('Favoritos list cargando...');
      this._favoritoService.getFavoritos().subscribe(
         //Compiler doesn't recognize getFavoritos()            
         result => {
            console.log(result);
         },
         error => {
            this.errorMessage = <any>error;
            if (this.errorMessage != null) {
                console.log(this.errorMessage);
                alert('Error en la petición');
            }
         }
     );
  }
 }

预先感谢

我在此链接中找到了解决方案:

https://www.reddit.com/r/angular2/comments/6ovswj/property_get_does_does_not_exist_on_type_type_typeof/

我放在的组件构造器上:

private _favoritoService = FavoritosService;    

而不是:

private _favoritoService : FavoritosService;    

我用于用Angular 2插件(例如TSLINT)编码Visual Studio代码。我很惊讶此行中没有显示任何错误

相关内容

  • 没有找到相关文章

最新更新