我的 TypeScript 代码有问题,我的 forEach 循环在页面上显示未定义。下面提供的是我的代码 for is my forEach 语句:
private _renderList(items: ISPList[]): void {
let html: string = '<div class="ms-Grid"><div class="ms-Grid-row">';
items.forEach((item: ISPList) => {
html += `
<div class="ms-Grid-col ms-u-sm4 ms-u-md4 ms-u-lg4">
<h2>${item.DisplayListTitle}</h2>
<div>${item.ListItems.length}</div>
<div>${item.ListItems.forEach((listItem: ListObject,index) => {
})}</div>
</div>`;
});
html +="</div></div>";
const listContainer: Element = this.domElement.querySelector('#spListContainer');
listContainer.innerHTML = html;
}
数据正在通过我的 _getMockListData 方法填充(见下文):
private _getMockListData(): Promise<ISPLists> {
return MockHttpClient.get()
.then((data: ISPList[]) => {
var listData: ISPLists = { value: data };
return listData;
}) as Promise<ISPLists>;
}
MockHttpClient 对象定义如下:
export default class MockHttpClient {
//Add some mock data to this for testing.
private static _createListItems: ListObject[] = [
{Title:'Doc1', FileType:'docx',ItemUrl:'http://www.espn.com', ItemType:'Document Library'},
{Title:'Doc2', FileType:'docx',ItemUrl:'http://www.espn.com', ItemType:'Document Library'}];
public static _applyListItems: ListObject[] = [
{Title:'ListItem 1', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'},
{Title:'ListItem 2', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'}];
public static _deployListItems: ListObject[] = [
{Title:'ListItem 2', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'},
{Title:'ListItem 3', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'}];
public static _supportListItems: ListObject[] = [
{Title:'ListItem 5 ', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'},
{Title:'ListItem 6', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'}];
private static _items: ISPList[] = [
{ Title: 'Mock List', Id: '1', ListType:"List", DisplayListTitle:'CREATE', RowCount: 4, SortOrder:1, ShowOnHomePage:true,ListItems:MockHttpClient._createListItems},
{ Title: 'Mock List 2', Id: '2', ListType:"Library", DisplayListTitle:'APPLY', RowCount: 4, SortOrder:2, ShowOnHomePage:true,ListItems:MockHttpClient._applyListItems },
{ Title: 'Mock List 3', Id: '3', ListType:"List", DisplayListTitle:'DEPLOY',RowCount: 4, SortOrder:3, ShowOnHomePage:true,ListItems:MockHttpClient._deployListItems },
{ Title: 'Mock List 4', Id: '4', ListType:"Library", DisplayListTitle:'SUPPORT', RowCount: 4, SortOrder:4, ShowOnHomePage:true,ListItems:MockHttpClient._supportListItems }];
public static get(): Promise<ISPList[]> {
return new Promise<ISPList[]>((resolve) => {
resolve(MockHttpClient._items);
});
}
}
下面提供了 ISPList 和 ListObject 的定义:
export interface ISPLists { value: ISPList[]; }
export interface ISPList {
Title: string;
Id: string;
DisplayListTitle: string;
RowCount: number;
SortOrder: number;
ShowOnHomePage: boolean;
ListType: string;
ListItems: Array<ListObject>;
}
export interface ListObject {
Title: string;
ItemUrl: string;
ItemType: string;
FileType: string;
}
我可以显示 item.ListItems.length 属性,它表明我有 2 个元素,另外我可以运行 item.ListItems[0].Title 来显示实际的标题值。感觉就像我需要一个 for 循环而不是一个 forEach 循环。
请帮忙。