在我的 Angular 應用程式中,我有一個簡單的 ngFor 回圈,顯示如下徽標影像:
<div *ngFor="let item of list" class="logo-wrapper">
<div class="customer-logo">
<span
class="my-icon"
aria-label="My icon"
[inlineSVG]="'./assets/image/projects/logo/' item.logo"
></span>
</div>
</div>
這作業正常!但是:如果我嘗試對陣列進行切片以限制輸出,如下所示:
<div *ngFor="let item of list | slice: 0:10; let i = index" class="logo-wrapper">
<div class="customer-logo">
<span
class="my-icon"
aria-label="My icon"
[inlineSVG]="'./assets/image/projects/logo/' item.logo"
></span>
</div>
</div>
我收到此錯誤:“物件的型別為‘未知’ ”。
錯誤輸出:

我真的不知道我在這里做錯了什么。我希望有人能指出我正確的方向。
編輯:一旦我向回圈添加索引,問題就會出現。我試圖將索引添加到物件中,例如:item.i.logo但它也是未知的。
PS:這是我的 .ts 檔案
@Component({
selector: 'app-logo-section',
templateUrl: './logo-section.component.html',
styleUrls: ['./logo-section.component.scss']
})
export class LogoSectionComponent implements OnInit {
list : any
constructor(
) {
this.list = getProjects()
console.log(this.list)
}
ngOnInit(): void {
}
private services = [{
slug : "s-l-u-g",
name : "name",
work : "work",
company : "company",
website : "https://www.google.com",
preview : "text",
logo : "logo.svg"
}]
getProjects(){
return services
}
}
uj5u.com熱心網友回復:
您必須將 的型別更改為list,any[]而不是any。在您的打字稿檔案中按如下方式更新宣告。
list : any[];
uj5u.com熱心網友回復:
似乎SlicePipe不推薦使用該ng-inline-svg包,因為它使用HttpClientModule并有效asynchronously。
如果你使用Array.slicemethod 而不是SlicePipein*ngFor它作業正常。
請找到Stackblitz 示例。
<div *ngFor="let item of list.slice(0, 10); let i = index" class="logo-wrapper">
<div class="customer-logo">
<span class="my-icon" aria-label="My icon" [inlineSVG]="item.logo"> </span>
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/366388.html
