2016-10-05 5 views
0

(これは私のグッズコンポーネントである私は、角2に非常に新たなんだと私は角1角度2プロパティ「X」はタイプ上に存在しない「Y」

に以前に書かれたアプリケーションをリファクタリングしようとしています

export interface Goodie { 
    status: string 
    pages: number 
    count: number 
    count_total: number 
    posts: any 
} 

マイグッズサービスエンドポイント

からデータをフェッチするために:魔女は、基本的に

import { Component, OnInit } from '@angular/core'; 
import { ConfigService } from '../config/config.service'; 
import { FetchGoodiesService } from '../shared/fetch-goodies.service' 
import { Goodie } from '../shared/goodie' 



@Component({ 
    selector: 'app-goodies', 
    templateUrl: './goodies.component.html', 
    styleUrls: ['./goodies.component.css'], 
    providers: [FetchGoodiesService, ConfigService], 
}) 
export class GoodiesComponent implements OnInit { 

    private goodies: Goodie[] = [] 
    private endpoint: string 

    constructor(private _fetchGoodiesService : FetchGoodiesService) { } 

    public ngOnInit() { 
     this._fetchGoodiesService.getGoodies().subscribe(data => { 
      this.goodies = data.posts 
      console.log("this goodies", data) 
     }); 
    } 

} 

)ワードプレスのブログのグッズカテゴリからフェッチされた投稿が、私は私のグッディ・インターフェースを持って示してい

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 
import { ConfigService } from '../config/config.service' 
import { Goodie } from '../shared/Goodie' 

import 'rxjs/Rx'`enter code here` 
import 'rxjs/add/operator/map' 


@Injectable() 
export class FetchGoodiesService { 

    private baseUrl: string 
    private endpoints: any 
    constructor(
     private _http: Http, 
     private _configService: ConfigService, 
    ) { 
     this.endpoints = this._configService.getProperty("endpoints") 
     this.baseUrl = this.endpoints.baseUrl 
    } 

    public getGoodies(): Observable<Goodie[]> { 
     return this._http.get(this.baseUrl + this.endpoints.getCategoryPost + 'goodies') 
       .map(res => res.json()) 
    } 


    private handleError(error) { 
     console.log(error) 
    } 

} 

、これは私がエンドポイントからrecivingてるJSONのモックです

{ 
    "status": "ok", 
    "count": 9, 
    "count_total": 9, 
    "pages": 1, 
    "posts": [ 
     {..}, 
     {..} 
    ] 
} 

私の問題は、コンパイラがグッズコンポーネントのこの行に私にエラーを返すということです。 this.goodies = data.posts私は次のようにしています:プロパティ '投稿'がタイプ 'Goodie []'に存在しません

投稿を宣言していると思っていました。 私を助けることができる誰かに感謝します。

+0

は、エラーが ではなく、 'this.goodies = data.posts' –

+0

感謝のこの1' this.goodies = data'を試してみてください、あなたのコードにここにあるようですしかし、私はthis.Goodiesには投稿が含まれているので、それらを循環させることができるようにする必要があります。 – freecloud

+0

'.map(res => res.json())'を '.map(res => res.json投稿) '? – cartant

答えて

0

私はアプリを機能させるウォークアラウンドを見つけましたが、あまり好きではありません。私は問題に明確な解決策があるのだろうかと思っています。

これはwalkaroundコードです:

export class GoodiesComponent implements OnInit { 
    private res:any 
    private goodies: Goodie[] = [] 
    private endpoint: string 

    constructor(private _fetchGoodiesService : FetchGoodiesService) { 
    } 

    public ngOnInit() { 
     this._fetchGoodiesService.getGoodies().subscribe(data => { 
      this.res = data 
      this.goodies = this.res.posts 
      console.log("this goodies", data) 
     }); 
    } 

} 
+0

これはしません私はまずこの 'this.res'に' data'を割り当ててから、そのグッズに投稿すると思いますか?ではない ? –

関連する問題