に存在している私のcomponent.ts
ファイルのGoogleスプレッドシートでの私のservice.ts
ファイル購読値component.tsにundefinedを返しますが、値はここでservice.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class GoogleSheetsService {
constructor(
private http: Http
) { }
getImages(sheetName) {
const apiServerEndPoint = '/api' + sheetName;
return this.http.get(apiServerEndPoint)
.map((res: Response) => {
console.log(res.json());
res.json();
});
}
}
解像度の
import { Component, OnInit } from '@angular/core';
import { GoogleSheetsService } from '../shared/services/googlesheets.service';
@Component({
selector: 'home-component',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
apiPortfolioListEndPoint: string;
portfolioList: Array<string>;
constructor (
private googleSheetsService: GoogleSheetsService
) {}
ngOnInit() {
this.apiPortfolioListEndPoint = '/home/portfolio';
this.getImagesFromSheets(this.apiPortfolioListEndPoint);
}
getImagesFromSheets(sheetName) {
this.googleSheetsService.getImages(sheetName)
.subscribe(photos => {
console.log(photos);
});
}
}
コンテンツの内容はサービスは値の配列を返し、コンソールに出力されますが、コンポーネントに登録すると未定義に戻ります(つまり、写真はコンソールで未定義に戻ります)。
getImages()
は、Googleスプレッドシートからデータを取得するAPIを呼び出します。
portfolioList
変数に写真を割り当てようとしたとき、atomは次のエラー"Type 'void' is not assignable to type 'string[]' "
を強調表示します。写真は変数に割り当てることができませんが、私はこの問題を回避するために頭を抱えているように見えます。
ご意見やご指摘をいただければ幸いです。
hi Shanil。あなたが正しい!リターンを加えて問題を解決しました!しかし、矢印の関数を使用してリターンを示していないのですか?私は間違って矢印の機能の使用を理解していますか? – eugeneoei
@eugeneoeiいいえ、それはありません。括弧を使用しない '{}'は復帰を示します。例: '.map((res:Response)=> res.json());' – echonax
@echonax ahhhhh。わかった。このような根本的な間違い。感謝echonaxと@シャニル! – eugeneoei