【发布时间】:2021-01-22 06:29:26
【问题描述】:
我正在尝试使用服务从服务器检索数据,我需要将值从我的服务传递到我的页面。我不确定这是否是满足我要求的合适方法。请帮忙!
cart.service.ts
export class CartService {
public jProducts="";
constructor(private http:HttpClient) { }
getMyProducts(){
const getValues = "http://website.com/temp.xq";
this.http.get<any>(getValues, {responseType: 'text' as 'json'})
.subscribe(
(res:any) => console.log(this.jProducts = (res))
);
return this.jProducts
}
}
mypage.ts
import { CartService } from 'src/app/services/cart.service';
loadValues: any= [];
ngOnInit() {
this.loadValues = this.cartService.jrnProducts;
}
【问题讨论】:
-
jrnProducts是什么?删除 return 关键字只需添加this.http.get<any>(getValues, {responseType: 'text' as 'json'}) .subscribe( (res:any) => this.jProducts = res; ); -
哎呀,它是来自服务的 jProducts。让我试试你的解决方案
标签: angular ionic-framework httpclient