如问题所述,我的post模块中有一个组件,即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
中以 /博客的形式打开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)
}
}
请指导我做错了什么。我只是想不通。
答案 0 :(得分:1)
我认为您需要以不同的方式加载模块 在 app.module.ts
中const routes: Routes = [{
path: 'blog',
loadChildren: () => import('./modules/posts/posts.module').then(m => m.PostsModule),
},
{
path: '',
redirectTo: '/blog',
pathMatch: 'full'
},
{
path:'dashboard',
component:PostDashboardComponent
}]
注意:在Angular 8及更高版本中,我们使用了另一种加载模块的方式,您使用的方式是:./posts/posts.module#PostsModule
是旧的方式,请参见Angular docs。
并将您的 post.module.ts 路线更改为
const routes: Routes = [
{path:'',component:PostListComponent},
{path:':id',component:PostDetailComponent}
]
答案 1 :(得分:-1)
尝试这样
ngOnInit() {
this.postService.getPosts().subscribe((response: any)=> {
this.posts = response;
})
}
答案 2 :(得分:-1)
this.postService.getPosts().subscribe((posts) => this.posts = posts);
可观察物是懒惰的。请订阅以启动它们。
答案 3 :(得分:-1)
Change the
{ path: '', redirectTo: '/blog', pathMatch: 'full'},
{ path: '', loadChildren: './posts/posts.module#PostsModule' }
to
{ path: 'blog', loadChildren: './posts/posts.module#PostsModule' }
You can remove the { path: '', redirectTo: '/blog', pathMatch: 'full'} .
Assuming the './posts' path is correct.
When the route matches with /blog it checks in the postmodule routing
,it checks with
const routes: Routes = [
{path:'blog',component:PostListComponent},
{path:'blog/:id',component:PostDetailComponent},
{path:'dashboard',component:PostDashboardComponent}
]
and serves the PostListComponent