我已經學習 angular 幾天了,我正在嘗試重新創建 Instagram 的部分副本以進行訓練。但是我面臨一個非常簡單的問題,我在尋找答案時在其他地方見過幾次-就像這個- 但他們都沒有幫助我。
我的問題如下:我需要通過回圈顯示縮略圖串列,并為影像源提供顯示影像的路徑,但我得到:錯誤 TS2551:“ListComponent”型別上不存在屬性“post”。您指的是 'posts' 嗎?
我現在的檔案:list.component.html:
<div class="list">
<div class="wrapper">
<div class="list-container">
<div class="thumbnail" *ngFor="post of posts">
<img src="{{post.image}}" alt="">
<!-- or <img [src]="post.image">-->
</div>
</div>
</div>
</div>
串列.component.ts:
import { Component, OnInit } from '@angular/core';
import { Post } from 'src/app/models/post.model';
import { PostsListService } from 'src/app/services/posts-list.service';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {
posts!: Post[];
constructor(private postsList: PostsListService) { }
ngOnInit(): void {
this.posts = this.postsList.getPosts();
}
}
我從posts-list.service.ts獲取我的資料:
import { Injectable } from '@angular/core';
import { postsData } from '../data';
import { Post } from '../models/post.model';
@Injectable({
providedIn: 'root'
})
export class PostsListService {
private posts!: Post[];
constructor() {
this.posts = postsData;
}
getPosts(): Post[]{
return this.posts;
}
}
也許這是變數范圍的問題,但既然其他人一直在使用它,為什么我不能呢?
以防萬一,這是我的帖子模型:post.model.ts:
export interface Post{
id:number;
userName: string;
image: string;
userImage: string;
description: string;
datePosted: Date;
location: string;
likesAmount: number;
}
uj5u.com熱心網友回復:
你錯過了回圈中的“讓”:
*ngFor="let post of posts"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/400339.html
上一篇:Firebase身份驗證隨機令牌
下一篇:錯誤TS2307:找不到模塊“./portfolio/portfolio.component”或其相應的型別宣告
