我正在尝试使用 Angular 2 和 Firebase 构建一个简单的博客,但在组件中使用异步管道时遇到问题。我在控制台中收到错误。
zone.js:344Unhandled Promise 拒绝:模板解析错误:找不到管道“异步”(“
[错误->]{{ (blog.user | async)?.first_name }}
"): BlogComponent@6:3 ; 区域: ; 任务: Promise.then ; 值: 错误: 模板解析错误:(...) 错误: 模板解析错误: 找不到管道'async' ("
blog.component.ts
import {Component, Input} from "@angular/core";
@Component({
selector: 'blog-component',
templateUrl: './blog.component.html',
styleUrls: ['./blog.component.css'],
})
export class BlogComponent {
@Input() blog;
}
blog.component.html
<h1 class="article-title">{{ blog.title }}</h1>
<p>{{ (blog.user | async)?.first_name }}</p>
app.component.ts
import { Component } from '@angular/core';
import { BlogService } from "./services/services.module";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private blogService: BlogService) {}
articles = this.blogService.getAllArticles();
}
app.component.html
<article *ngFor="let article of articles | async">
<blog-component [blog]="article"></blog-component>
</article>
博客服务.ts
import {Injectable} from "@angular/core";
import {AngularFire} from "angularfire2";
import {Observable} from "rxjs";
import "rxjs/add/operator/map";
@Injectable()
export class BlogService {
constructor(private af: AngularFire) { }
getAllArticles(): Observable<any[]> {
return this.af.database.list('articles', {
query: {
orderByKey: true,
limitToLast: 10
}
}).map((articles) => {
return articles.map((article) => {
article.user = this.af.database.object(`/users/${article.user_id}`);
return article;
});
});
}
}
仅当我尝试在 blog.component.html 文件中使用异步时才会出现问题。如果我尝试在 app.component.html 文件中打印用户名,它会起作用。我应该在 blog.module.ts 中注入 AsyncPipe 吗?如何让异步在 blog.component.ts 中工作?