我得到一個奇怪的錯誤與RxJS,得到'方法不是一個函數'。RxJS Observable:'跳過不是函數'(或任何其他函數)
我確實導入了Observable庫。 我已經得到Observable數組沒有提出錯誤。
但是,當我做一個跳過,或採取,我得到這個錯誤。
如果我正確地讀出:
返回一個可觀察的是跳過由 源可觀察到發射的第一計數的項目。
正如我觀察到的包含文章,它應該在可觀測直接跳到X條?
下面是代碼
import { Component, OnInit } from '@angular/core';
import { Article} from '../../model/article';
import { ArticleService} from '../../model/article.service';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Component({
selector: 'app-liste-article',
templateUrl: './liste-article.component.html',
styleUrls: ['./liste-article.component.css']
})
export class ListeArticleComponent implements OnInit {
articles: Observable<Article[]>;
articlesPage: Observable<Article[]>;
selectedArticle: Article;
newArticle: Article;
page = 1;
itemPerPage = 2;
totalItems = 120;
constructor(private router: Router, private articleService: ArticleService) {
}
ngOnInit() {
this.articles = this.articleService.getArticles();
this.articlesPage = this.articles.skip((this.page - 1) * this.itemPerPage).take(this.itemPerPage);
//this.articlesPage = this.articles.slice( (this.page - 1) * this.itemPerPage , (this.page) * this.itemPerPage);
}
onSelect(article: Article) {
this.selectedArticle = article;
this.router.navigate(['../articles', this.selectedArticle.id ]);
}
onPager(event: number): void {
console.log('Page event is' , event);
this.page = event;
this.articlesPage = this.articles.skip((this.page - 1) * this.itemPerPage).take(this.itemPerPage);
console.log('tab' , this.articlesPage.count());
//this.articleService.getArticles().then(articles => this.articles = articles);
}
getArticles(): void {
this.articles = this.articleService.getArticles();
}
createArticle(article: Article): void {
//this.articleService.createArticle(article)
//.then(articles => {
// this.articles.push(articles);
// this.selectedArticle = null;
//});
}
deleteArticle(article: Article): void {
//this.articleService
// .deleteArticle(article)
// .then(() => {
// this.articles = this.articles.filter(b => b !== article);
// if (this.selectedArticle === article) { this.selectedArticle = null; }
//});
}
}
我試圖直接與.skip(1)
使用它,但得到了同樣的錯誤。
我查了官方文件RxJS Observable,它對我來說很合適。看來情況是好的,就像我對該函數的調用一樣。這兩個arryas都是可觀察的,所以我應該能夠在一個上使用這些函數,並將結果放在另一個正確的位置?
我沒有看到這裏有什麼問題,可能是一個小細節,但不能親自看到它(因爲我從這開始,我看不到更多細節)。
我的直覺懷疑,你應該這樣做'進口「rxjs /添加/運營/ skip'但我可能是錯的。 –
確實。正如UncleDave所說的那樣。我認爲它被包含在Observable Import中。 –
你能分享this.articleService.getArticles()代碼嗎? –