我仍然对rxjs如何工作感到困惑.
我正在构建一个Ionic应用程序,它向我的服务器发出请求并期望json.我已经成功订阅了http.post并获取了我需要的数据.
但是现在我的问题是我需要在我从Storage获得的http请求中传递一个auth令牌.这是一个问题,因为我需要等到存储准备就绪,然后在调用http.post请求之前从中获取我的令牌值.
这是我试图获取我的json数据的地方
getPlanograms() {
//API URL
let requestURL = 'https://myapiurlhere';
let headers = new Headers({'Content-Type': 'application/json'});
return this.storage.ready().then(() => {
return this.storage.get('id_token').then((val) => {
headers.append('Authorization', 'Bearer ' + this.authCredentials.token);
let options = new RequestOptions({headers: headers});
return this.http.post(requestURL, {}, options)
.map(response => <Planogram[]>response.json());
})
});
}
Run Code Online (Sandbox Code Playgroud)
从这里开始调用
ionViewDidLoad (){
this.merchandisingDataService.getPlanograms()
.subscribe(Planogram => this.planograms = Planogram);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这样做时,我得到以下错误
"承诺"类型中不存在"订阅"属性.
实现目标的最佳方式是什么?
使用Cameara拍照时destinationType: this.camera.DestinationType.FILE_URI,生成的URL无法显示图像.例如,在尝试拍摄这样的照片时:
this.camera.getPicture(options).then((url) => {
// Load Image
this.imagePath = url;
}, (err) => {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
试图显示它<img [src]="imagePath" >会导致错误(找不到文件).
这里的问题是URL在file:///storage...路径中而不是基于localhost的正确路径.
我的Laravel 5.5应用程序中有很多API资源。到目前为止,它们很棒,但是在分页链接中保留URL参数时遇到了问题。
请参见下面的URL示例:/ posts?unreviewed = true
public function getPosts(Request $request){
/*
* Gets a list of posts.
*
* Options:
* - unreviewed: gets posts without revisions (default: false)
*
*/
$pagination = 20;
//Check constrains
if($request->unreviewed == true){
return SocialPostResource::collection(SocialPost::with(['images', 'publication.images'])
->doesntHave('revisions')
->paginate($pagination));
}
return SocialPostResource::collection(SocialPost::with(['images', 'publication.images'])->paginate($pagination));
}
Run Code Online (Sandbox Code Playgroud)
以下示例仅包含已修订的帖子。这在第一个查询中效果很好。问题在于分页结果在URL中不包含“ reviewed = true”参数,因此第2页及以后的页面将返回所有帖子。我需要所有URL都包括原始请求中传递的任何参数。
“data”:{...},
“links”:{
...
“next”: “/posts?page=2”
}
Run Code Online (Sandbox Code Playgroud)
我期望的结果是“ / posts?unreviewed = true&page = 2”
所以我有一个 Laravel API 资源,它返回模型的标准数据库信息。但是,在某些情况下,我需要此资源来呈现一些触发复杂/缓慢查询的数据。
当我需要这些数据时,我不介意花费更长的时间,但由于大多数用例不需要它,所以我想使用$this->when()资源中的方法有条件地呈现它。
class SomeResource extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'created_at' => $this->created_at,
//The "slow_query()" should not be executed
'slow_result' => $this->when(false, $this->slow_query()),
];
}
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,即使条件when为假,慢速查询仍然会执行,尽管结果从未显示并且查询毫无用处。
如何slow_query()在不需要时阻止该方法运行?
这是 Laravel 5.8 上的