我正在從http請求獲取對象。我使用put方法通過另一個http請求存儲該對象。我想在存儲該對象之前使用Google翻譯API將該對象中的一些字符串翻譯爲不同的語言。我怎樣才能做到這一點。如何在從http請求獲取對象之後從對象中轉換多個字符串,然後使用轉換後的字符串存儲該對象
service.ts
import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Rx'; import { BaseApi } from '../../../../laas/base-api.service'; import { ApplicantService } from '../../../../laas/applicant.service'; import { environment as ENV } from '../../../../../environments/environment' @Injectable() export class NregaService { private applicantId: string; private baseUrl: string; constructor( private http: HttpClient, private api: BaseApi, private applicant: ApplicantService ) { this.applicantId = applicant.getApplicantID() this.baseUrl = `/applicants/${this.applicantId}/id/nrega`; } get() { return this.api.newGet(this.baseUrl); } save(type, data) { return this.api.put(this.baseUrl, { type: type, data: data }); } translate(q) { const translateUrl = ENV.googleAddress.translateUrl; const apiKey = ENV.googleAddress.apiKey; const target = 'en'; const model = 'base'; return this.http.post(`${translateUrl}${apiKey}`, { q, target, model }).map(res => { const value = res['data'].translations[0]; console.log(`Translated ${q}:`, value); return value.translatedText; }); } verify(id) { return this.api.post('nrega', { jobcardid: id }).map(result => { if (typeof result === typeof '') { throw result; } const incomes = []; if (result.incomeDetail) { result.incomeDetail .sort((a, b) => a.year <= b.year ? -1 : 1) .forEach(income => incomes.push({ income: income['income(Rs.)'], year: income.year })); } const details = []; if (result.applicantDetail) { result.applicantDetail.forEach(detail => details.push({ name: detail.name, gender: detail.gender[0].toLowerCase(), age: detail.age, bankOrPostOffice: detail.bankorpostoffice, aadhaarNo: detail.aadhaarNo, accountNo: detail.accountNo })); } const data = { jobCardId: result.jobcardno, regDate: result.dateOfRegistration, photo: result.photoImageUrl, voterId: result.voterId, fatherOrHusband: result.nameOfFatherOrHusband, family: { ids: [result.familyId1, result.familyId], members: details, head: result.nameOfHead, isBpl: result.bplFamily.toLowerCase() === 'yes' ? true : false, bplId: result.bplFamilyId, }, address: { full: result.address, village: result.village, district: result.district, panchayat: result.panchayat, block: result.block }, category: result.category, incomes: incomes, }; console.log('NREGA: ', data); return data; }); } }
如果我使用也許是因爲我沒有使用.subscribe方法(我使用的部件,其中翻譯方法翻譯服務字符串本身然後我得到錯誤我在調用服務的verify()方法。在將數據返回到組件之前,是否可以將多個字符串轉換爲服務本身?