最佳答案
我正在尝试用 Angular 2和 Firebase 构建一个简单的 blog,我在组件中使用异步管道时遇到了问题。我在控制台中得到错误。
拒绝未处理的承诺: 模板解析错误: 无法找到管道’异步’(”
[ ERROR-> ]{{(blog.user | sync) ? . first _ name }}
值: 错误: 模板解析错误: (...)错误: 模板解析错误: 无法找到管道’异步’(”
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;
}
博客. 组件. html
<h1 class="article-title">{{ blog.title }}</h1>
<p>{{ (blog.user | async)?.first_name }}</p>
应用程序组件
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>
Blog.service.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 中工作?