我正在开发一个Angular应用程序,在这里,我使用HttpClient
模块来使用API响应。我已经开发了一些东西,但我总是得到#34; undefined"作为回应。
这里我有两个组件和一个服务文件,从Landingpage.component.ts我有onclick
事件将传递"产品名称"服务文件,从服务文件(cartdata.service.ts)我将产品名称传递给API
API将返回特定产品的图像路径。我在服务中接收API响应并处理它然后将数据传递给该组件的mycart.component.ts组件我将路径分配给受尊重的HTML页面
我想要做的是,从API响应中获取特定产品的所有图像路径,并将其分配给受尊重的HTML页面。
landinpage.component.ts - cartdata.service.ts -my-cart.component.ts-HTMLpages API响应:
这是我从API收到的回复。
This is my landingpage.components.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CartdataService } from '../../services/cartdata.service';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-landingpage',
templateUrl: './landingpage.component.html',
styleUrls: ['./landingpage.component.css']
})
export class LandingpageComponent {
product_Name: any;
ngOnInit() { }
constructor(private CartdataService: CartdataService, private router: Router{}
getProductName(Pname: any) {
this.CartdataService.get_Product_Path(Pname.textContent);
}
}
这是我的cartdata.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class CartdataService {
public i_product_Path = new BehaviorSubject<any>('');
i_cast_Product_Path = this.i_product_Path.asObservable();
public j_product_Path = new BehaviorSubject<any>('');
j_cast_Product_Path = this.j_product_Path.asObservable();
public k_product_Path = new BehaviorSubject<any>('');
k_cast_Product_Path = this.k_product_Path.asObservable();
public Count = new BehaviorSubject<number>(0);
cast = this.Count.asObservable();
currentCount :number = 0;
current_product :any;
i_COUNTER :number;
j_COUNTER :number;
k_COUNTER :number;
big_Image_Path:string[][];
small_Image_Path:string[][];
selected_Product_Image: string[][];
constructor(private http: HttpClient) { }
editCount(newCount: number) {
this.currentCount += newCount;
this.Count.next(this.currentCount);
}
get_Product_Path(pName: string) {
this.current_product = pName.trim();
this.http.get(`http://localhost:abc/api/data/GetImage/?imageName=${this.current_product}`)
.subscribe(data => {
this.i_COUNTER = data[0].Count;
this.j_COUNTER = data[1].Count;
this.k_COUNTER = data[2].Count;
if(this.i_COUNTER >0) {
let i:number;
for( i=0;i<=this.i_COUNTER;i++){
this.big_Image_Path =data[0]['big_Images'];
}
}
if(this.j_COUNTER >0){
let j:number;
for( j=0;j<=this.j_COUNTER;j++){
this.small_Image_Path =data[1]['small_Images'];
}
}
if(this.k_COUNTER >0){
let k:number;
for( k=0;k<=this.k_COUNTER;k++){
this.selected_Product_Image =data[2]['selected_Product_Image']
}
}
this.i_product_Path.next(this.big_Image_Path);
this.j_product_Path.next(this.small_Image_Path);
this.k_product_Path.next(this.selected_Product_Image);
});
}
}
这是我的my-cart.component.ts
import { Component, EventEmitter, Output } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { CartdataService } from '../../services/cartdata.service';
import { Http } from '@angular/http';
@Component({
selector: 'app-my-cart',
templateUrl: './my-cart.component.html',
styleUrls: ['./my-cart.component.css'],
outputs: ['ChildEvent']
})
export class MyCartComponent {
nCount: number;
product_Name: any;
i_path: string[][];
j_path: string[][];
k_path: string[][];
i_Counter :number;
i_bigImage_path:string[];
constructor(private CartdataService: CartdataService, private router: Router, private http: HttpClient) {
this.router.events.subscribe(
() => window.scrollTo(0, 0)
);
}
ngOnInit() {
this.CartdataService.cast.subscribe(totalItems => this.nCount = totalItems);
this.CartdataService.i_cast_Product_Path.subscribe(big_Image_Path => this.i_path[0] = big_Image_Path);
this.CartdataService.j_cast_Product_Path.subscribe(small_Image_Path => this.j_path[1] = small_Image_Path);
this.CartdataService.k_cast_Product_Path.subscribe(selected_Image_Path => this.k_path[2] = selected_Image_Path);
this.i_path[0][0]['big_Images'] = this.i_bigImage_path;
}
}
这里我需要将每个路径分配给一个局部变量,以便将路径传递给HTML页面。
答案 0 :(得分:0)
this.http.get(`http://localhost:abc/api/data/GetImage/?imageName=${this.current_product}`)
.subscribe(data => {
......
此处data
不是您期望的json有效负载。它是一个http响应,应首先转换为json对象。试试这个:
this.http.get(`http://localhost:abc/api/data/GetImage/?imageName=${this.current_product}`)
.map(r => r.json())
.subscribe(data => {
......
答案 1 :(得分:0)
首先导入&rxjs / Rx&#39;在服务类的顶部。然后使用map运算符并从服务返回响应作为json并处理组件中的json。 e.g:
getData(){
return this.http.get('http://localhost:abc/api/data/GetImage/?imageName=${this.current_product}')
.map((response: Response) =>{ return response.json()})
.catch((error: Response) => {
return Observable.throw(error);
});
}