Angular 7 - 对象的 Observables 数组的 Observable 数组
Angular 7 - Observable array of Observables arrays of an Object
我有以下问题,我的文章服务中有一系列文章 class,我需要在我的主页上按行显示 3 篇文章,所以我创建了一个数组按数组排列的 3 篇文章的数组。我的主页需要显示每篇文章,并在单击时路由到 articles/article:id。我的问题是,当我单击文章时,路线运行良好但文章不显示。
如果我在 localhost:4200/articles/1 刷新浏览器,它会完美显示 id = 1 文章的所有属性,但是当我在 localhost:4200/blog 并且我单击文章转到localhost:4200/articles/1,没有显示任何内容
文章class:
export class Article {
id: string;
title: string;
briefInfo: string;
topic: string;
author: string;
authorImg: string;
relatedArticlesId: string[];
mainImg: string;
bodyHtml: string;
date: string;
constructor() {
}
}
文章服务class:
arrayGroupedBy3: Article[][] = [];
getArticlesGroupedBy3(): Observable<Article[][]> {
if (this.articles.length > 3) {
while (this.articles.length > 0) {
this.articles.map( ( ): Article[] => {
return this.articles.splice(0, 3);
}).forEach( item => this.arrayGroupedBy3.push(item));
}
}
return of(this.arrayGroupedBy3);
}
getArticleById(id: string): Observable<Article> {
return of(this.articles.find(item => item.id === id));
}
文章列表组件class:
articlesOf3$: Observable<Article[][]>;
selectedId: string;
constructor(private articleService: ArticleService, private router: ActivatedRoute ) {
}
ngOnInit() {
this.getArticles();
}
getArticles() {
this.articlesOf3$ = this.router.paramMap.pipe(
switchMap( params => {
this.selectedId = params.get('id');
return this.articleService.getArticlesGroupedBy3();
}));
}
文章-list.component.html class:
<section class="row content_articles">
<article class="center_content">
<ul *ngFor="let listOf3articlesMax of articlesOf3$ | async"
class="row content_list articles">
<li *ngFor="let article of listOf3articlesMax"
[class.selected] = "article.id === selectedId"
class="{{article.topic}}">
<a [routerLink]="['/articles',article.id]">
<figure class="row article_img">
<img src="{{article.mainImg}}" alt="" title="">
</figure>
<div class="row content_information">
<!--Tag-->
<span class="content_tag">{{article.topic}}</span>
<div class="row content_text">
<h4>{{article.title}}:</h4>
<p>{{article.briefInfo}}</p>
</div>
</div>
<div class="row author_information">
<figure>
<img src="{{article.authorImg}}" alt="" title="" />
</figure>
<div class="author">
<h5>by {{article.author}}</h5>
<span class="date">{{article.date}}</span>
</div>
</div>
</a>
</li>
</ul>
</article>
</section>
article.component class:
article: Article;
article$: Observable<Article>;
constructor(
private articleService: ArticleService,
private route: ActivatedRoute,
private location: Location,
) { }
ngOnInit(): void {
this.getArticleById();
}
getArticleById(): void {
this.article$ = this.route.paramMap.pipe(
switchMap((params: ParamMap) => this.articleService.getArticleById(params.get('id'))));
}
goBack(): void {
this.location.back();
}
最后 article.component.html class:
<section *ngIf="article$" class="row content_blog_detail {{article$.topic}}">
<div class="row content_article">
<!--Tag-->
<span class="content_tag">{{article$.topic}}</span>
<!--Titles-->
<h1 class="row">{{article$.title}}</h1>
<h2 class="row">{{article$.briefInfo}}</h2>
<!--Return-->
<a (click)="goBack()" class="btn_return">Back</a>
</div>
</section>
应用路由模块:
const routes: Routes = [
{path: 'blog', component: ArticleListComponent},
{path: 'articles/:id', component: ArticleComponent}
];
其实3篇文章的列表显示还可以,但是当点击一篇文章跳转到该文章id的路由时,显示文章详情none。
您的 article$
属性 是一个 Observable。您需要先订阅它,然后才能在文章组件中访问文章对象的属性。
您应该使用 Angular 的 async
管道来呈现文章:
<section *ngIf="article$ | async as article" class="row content_blog_detail {{article.topic}}">
<div class="row content_article">
<!--Tag-->
<span class="content_tag">{{article.topic}}</span>
<!--Titles-->
<h1 class="row">{{article.title}}</h1>
<h2 class="row">{{article.briefInfo}}</h2>
<!--Return-->
<a (click)="goBack()" class="btn_return">Back</a>
</div>
</section>
我有以下问题,我的文章服务中有一系列文章 class,我需要在我的主页上按行显示 3 篇文章,所以我创建了一个数组按数组排列的 3 篇文章的数组。我的主页需要显示每篇文章,并在单击时路由到 articles/article:id。我的问题是,当我单击文章时,路线运行良好但文章不显示。
如果我在 localhost:4200/articles/1 刷新浏览器,它会完美显示 id = 1 文章的所有属性,但是当我在 localhost:4200/blog 并且我单击文章转到localhost:4200/articles/1,没有显示任何内容
文章class:
export class Article {
id: string;
title: string;
briefInfo: string;
topic: string;
author: string;
authorImg: string;
relatedArticlesId: string[];
mainImg: string;
bodyHtml: string;
date: string;
constructor() {
}
}
文章服务class:
arrayGroupedBy3: Article[][] = [];
getArticlesGroupedBy3(): Observable<Article[][]> {
if (this.articles.length > 3) {
while (this.articles.length > 0) {
this.articles.map( ( ): Article[] => {
return this.articles.splice(0, 3);
}).forEach( item => this.arrayGroupedBy3.push(item));
}
}
return of(this.arrayGroupedBy3);
}
getArticleById(id: string): Observable<Article> {
return of(this.articles.find(item => item.id === id));
}
文章列表组件class:
articlesOf3$: Observable<Article[][]>;
selectedId: string;
constructor(private articleService: ArticleService, private router: ActivatedRoute ) {
}
ngOnInit() {
this.getArticles();
}
getArticles() {
this.articlesOf3$ = this.router.paramMap.pipe(
switchMap( params => {
this.selectedId = params.get('id');
return this.articleService.getArticlesGroupedBy3();
}));
}
文章-list.component.html class:
<section class="row content_articles">
<article class="center_content">
<ul *ngFor="let listOf3articlesMax of articlesOf3$ | async"
class="row content_list articles">
<li *ngFor="let article of listOf3articlesMax"
[class.selected] = "article.id === selectedId"
class="{{article.topic}}">
<a [routerLink]="['/articles',article.id]">
<figure class="row article_img">
<img src="{{article.mainImg}}" alt="" title="">
</figure>
<div class="row content_information">
<!--Tag-->
<span class="content_tag">{{article.topic}}</span>
<div class="row content_text">
<h4>{{article.title}}:</h4>
<p>{{article.briefInfo}}</p>
</div>
</div>
<div class="row author_information">
<figure>
<img src="{{article.authorImg}}" alt="" title="" />
</figure>
<div class="author">
<h5>by {{article.author}}</h5>
<span class="date">{{article.date}}</span>
</div>
</div>
</a>
</li>
</ul>
</article>
</section>
article.component class:
article: Article;
article$: Observable<Article>;
constructor(
private articleService: ArticleService,
private route: ActivatedRoute,
private location: Location,
) { }
ngOnInit(): void {
this.getArticleById();
}
getArticleById(): void {
this.article$ = this.route.paramMap.pipe(
switchMap((params: ParamMap) => this.articleService.getArticleById(params.get('id'))));
}
goBack(): void {
this.location.back();
}
最后 article.component.html class:
<section *ngIf="article$" class="row content_blog_detail {{article$.topic}}">
<div class="row content_article">
<!--Tag-->
<span class="content_tag">{{article$.topic}}</span>
<!--Titles-->
<h1 class="row">{{article$.title}}</h1>
<h2 class="row">{{article$.briefInfo}}</h2>
<!--Return-->
<a (click)="goBack()" class="btn_return">Back</a>
</div>
</section>
应用路由模块:
const routes: Routes = [
{path: 'blog', component: ArticleListComponent},
{path: 'articles/:id', component: ArticleComponent}
];
其实3篇文章的列表显示还可以,但是当点击一篇文章跳转到该文章id的路由时,显示文章详情none。
您的 article$
属性 是一个 Observable。您需要先订阅它,然后才能在文章组件中访问文章对象的属性。
您应该使用 Angular 的 async
管道来呈现文章:
<section *ngIf="article$ | async as article" class="row content_blog_detail {{article.topic}}">
<div class="row content_article">
<!--Tag-->
<span class="content_tag">{{article.topic}}</span>
<!--Titles-->
<h1 class="row">{{article.title}}</h1>
<h2 class="row">{{article.briefInfo}}</h2>
<!--Return-->
<a (click)="goBack()" class="btn_return">Back</a>
</div>
</section>