【发布时间】:2019-12-30 09:36:29
【问题描述】:
所以正如问题所述,我的帖子模块中有一个组件,即 PostList。它应该做的是在用户访问网站时立即加载。 它的代码如下 posts-list.component.css
section {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px,1fr));
grid-template-rows: auto;
grid-gap: 10px;
margin: 0 auto;
max-width: 90%;
}
mat-card {
margin: 10px 0;
}
post-list.component.html
<section>
<mat-card *ngFor="let post of posts | async">
<mat-card-content routerLink="{{post.id}}">
<img mat-card-image src="{{post.image}}" alt="{{post.title}}">
<h2>{{post.title}}</h2>
<p>
<small>Posted by {{post.author}} • on {{post.published | date:"fullDate"}}</small>
</p>
</mat-card-content>
<mat-card-actions align="end" *ngIf="auth.currentUserId === post.authorId">
<button mat-icon-button (click)="delete(post.id)">
<mat-icon>delete</mat-icon>
</button>
</mat-card-actions>
</mat-card>
</section>
post-list.component.ts
import { Component, OnInit } from '@angular/core'
import { Observable } from 'rxjs/Observable'
import { PostService } from '../posts.service'
import { Post } from '../post'
import { AuthService } from '../../core/auth.service'
@Component({
selector: 'app-post-list',
templateUrl: './post-list.component.html',
styleUrls: ['./post-list.component.css']
})
export class PostListComponent implements OnInit {
posts: Observable<Post[]>
constructor(private postService: PostService, public auth: AuthService) {}
ngOnInit() {
this.posts = this.postService.getPosts()
}
delete(id: string) {
this.postService.delete(id)
}
}
由于它在模块内,我已将其路由为在 post.module.ts
中以 /blog 的形式打开const routes: Routes = [
{path:'blog',component:PostListComponent},
{path:'blog/:id',component:PostDetailComponent},
{path:'dashboard',component:PostDashboardComponent}
]
我不知道为什么它没有显示在网站上。
在 app.module.ts 中
const routes: Routes = [
{ path: '', redirectTo: '/blog', pathMatch: 'full'},
{ path: '', loadChildren: './posts/posts.module#PostsModule' },
]
posts.service.ts
import { Injectable } from '@angular/core'
import {
AngularFirestore,
AngularFirestoreCollection,
AngularFirestoreDocument
} from '@angular/fire/firestore'
import { Post } from './post'
import 'rxjs/add/operator/map'
@Injectable()
export class PostService {
postsCollection: AngularFirestoreCollection<Post>
postDoc: AngularFirestoreDocument<Post>
constructor(private afs: AngularFirestore) {
this.postsCollection = this.afs.collection('posts', ref =>
ref.orderBy('published', 'desc')
)
}
getPosts() {
return this.postsCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Post
const id = a.payload.doc.id
return { id, ...data }
})
})
}
getPostData(id: string) {
this.postDoc = this.afs.doc<Post>(`posts/${id}`)
return this.postDoc.valueChanges()
}
getPost(id: string) {
return this.afs.doc<Post>(`posts/${id}`)
}
create(data: Post) {
this.postsCollection.add(data)
}
delete(id: string) {
return this.getPost(id).delete()
}
update(id: string, formData) {
return this.getPost(id).update(formData)
}
}
请指导我做错了什么。我就是想不通。
【问题讨论】:
-
请订阅
this.postService.getPosts()邮递服务getPosts()方法 -
你的路由错误,见angular.io/guide/…
-
size的posts是什么? -
请发
PostService的代码 -
控制台有错误吗?
标签: javascript angular routing angular-router