I'm new to Angular and I believe I've set up my mock data the wrong way and that results in the following error:
Argument of type '{ "name": string; "refCode": string; "contentId": string; "enabled": boolean; }[]; } | { ...; }' is not assignable to parameter of type 'Item'.
Updated with additional error messages:
Types of property ... are incompatible.
Type '{ "name": string; "refCode": string; "contentId": string }[]' is not assignable to type 'Item[]'.
Property ... is missing in type '{ "name": string; "refCode": string; "contentId": string;}' but required in type 'Item'.
Here's my json file contents:
{
"data": {
"items": [
{
"obj1": "one",
"obj2": "two",
"obj3": three
},
{
"obj1": "four",
"obj2": "five",
"obj3": siix
"subCategories": [
{
}
]
},
{
"name": "Menu Option 1",
"refCode": "opt1",
"contentId": "0",
"subCategories": [
{}
]
},
{
"name": "Menu Option 2",
"refCode": "opt2",
"contentId": "0"
},
{
"name": "Menu Option 3",
"refCode": "opt3"
}
]
}
}
In my service class I import it like this:
import dataItems from './shared/my-items.json';
And declare it like this:
public menuItems: any;
Then I iterate over the items here in my component class:
const optionData =
this.itemService.getMenuItems().data.items;
const resource = {};
if (itemData && itemData.length > 0) {
this.items = [];
itemData.forEach(element => {
this.itemService.applyItemName(
element
);
this.items.push(element
});
this.itemService.categories = this.itemss;
}
Lastly is the applyItemName() method which adds an item and its children for a given Item:
public applyItemName(item: Item) {
if (item.subCategories && item.subCategories.length > 0) {
item.subCategories.forEach(subCategory => {
subCategory.parent = category;
this.applyItemName(subCategory);
});
}
}
The model for a Item object is here:
export class Category {
public subCategories: Items[];
public contentId: string;
constructor(subCategories: Item[], enabled: boolean, name: string)
{
this.subCategories = subCategories;
this.enabled = enabled;
this.name = name;
}
}
As I said, I'm new to this and I'm not sure what I'm missing. Any tips are greatly appreciated.