Javascript 在父方法内调用subscribe方法
我编写了一个通用方法,如下所示:Javascript 在父方法内调用subscribe方法,javascript,angular,typescript,rxjs,observable,Javascript,Angular,Typescript,Rxjs,Observable,我编写了一个通用方法,如下所示: getArticleById(loading: Loading): void { this.articleService.getArticleById(this.data.definition.id) .map((res: any) => res.json()) .subscribe((res: any) => { if (res.definition.is_purchased) {
getArticleById(loading: Loading): void {
this.articleService.getArticleById(this.data.definition.id)
.map((res: any) => res.json())
.subscribe((res: any) => {
if (res.definition.is_purchased) {
//more code
} else {
//more code
}
loading.dismiss();
} else {
loading.dismiss();
}
}, () => { loading.dismiss(); });
}
myParentMethod(){
const loading = this.loader.create({
content: 'loading...'
});
loading.present();
this.getArticleById(loading);//can I call the `loading.dismiss()` here.
}
getArticleById(loading: Loading) {
const articles$ = this.articleService.getArticleById(this.data.definition.id)
.map((res: any) => res.json());
article$.subscribe((res: any) => {
if (res.definition.is_purchased) {
//more code
} else {
//more code
}
});
return article$;
}
父方法(或调用)如下所示:
getArticleById(loading: Loading): void {
this.articleService.getArticleById(this.data.definition.id)
.map((res: any) => res.json())
.subscribe((res: any) => {
if (res.definition.is_purchased) {
//more code
} else {
//more code
}
loading.dismiss();
} else {
loading.dismiss();
}
}, () => { loading.dismiss(); });
}
myParentMethod(){
const loading = this.loader.create({
content: 'loading...'
});
loading.present();
this.getArticleById(loading);//can I call the `loading.dismiss()` here.
}
getArticleById(loading: Loading) {
const articles$ = this.articleService.getArticleById(this.data.definition.id)
.map((res: any) => res.json());
article$.subscribe((res: any) => {
if (res.definition.is_purchased) {
//more code
} else {
//more code
}
});
return article$;
}
我想从genric方法(
getArticleById()
)中删除加载参数,并需要在解析订阅后将其放入父方法(myParentMethod()
)中。您能告诉我如何操作吗 要处理在更高级别终止的可观察对象,您需要返回更高级别函数可以访问的可观察对象
试着这样写:
getArticleById(loading: Loading): void {
this.articleService.getArticleById(this.data.definition.id)
.map((res: any) => res.json())
.subscribe((res: any) => {
if (res.definition.is_purchased) {
//more code
} else {
//more code
}
loading.dismiss();
} else {
loading.dismiss();
}
}, () => { loading.dismiss(); });
}
myParentMethod(){
const loading = this.loader.create({
content: 'loading...'
});
loading.present();
this.getArticleById(loading);//can I call the `loading.dismiss()` here.
}
getArticleById(loading: Loading) {
const articles$ = this.articleService.getArticleById(this.data.definition.id)
.map((res: any) => res.json());
article$.subscribe((res: any) => {
if (res.definition.is_purchased) {
//more code
} else {
//more code
}
});
return article$;
}
最后
是一个有用的运算符
在源可观察序列正常或异常终止后调用指定的操作
然而,这段代码的结构仍然有点笨拙。我将把逻辑分离出来,从中获得可观察性并处理它,然后编写如下代码:
getArticleById(): Observable<Article> {
return this.articleService.getArticleById(this.data.definition.id)
.map(res => res.json());
}
handleArticle(article) {
if (article.definition.is_purchased) {
//more code
} else {
//more code
}
}
myParentMethod(){
const loading = this.loader.create({
content: 'loading...'
});
const article$ = this.getArticleById();
loading.present();
article$
.finally(() => loading.dismiss())
.subscribe(article => this.handleArticle(article));
}
getArticleById():可观察{
返回this.articleService.getArticleById(this.data.definition.id)
.map(res=>res.json());
}
手稿(文章){
if(是否购买了第条定义){
//更多代码
}否则{
//更多代码
}
}
myParentMethod(){
常量加载=this.loader.create({
内容:“正在加载…”
});
const article$=this.getArticleById();
loading.present();
文章$
.finally(()=>loading.Disclose())
.subscribe(article=>this.handleArticle(article));
}
你能告诉我你所说的$
标记(即文章$
)是什么意思吗?这是一个可观测的命名约定吗?我也想知道吗?有参考资料吗?谢谢。是的,这是一个可观测的命名约定。