1

I'm new to Angular and TypeScript so appologies if this is a stupid question. I'm trying to save the value of a returned object to a nested property in an object of type any. However, its not allowing me so could anyone enlighten me on my error.

  sharedData:any;

  constructor(private menuService: MenuService) { }

  addToBasket(item:any): void {
    this.sharedData.basketList.push(item);
  }

  ngOnInit() {
    this.sharedData.menu = this.menuService.getMenu();
  }

It works fine when I have it as this.menu = this.menuService.getMenu();

1 Answer 1

2

sharedData is not initialized with anything. from your code I would expect it to be

 sharedData:any = {
  basketList: []
 };

at least the lines you provided would run well and data would be stored in sharedData

1
  • Thank you so much. I didn't expect a reply within 2 mins to be honest but it's really appreciated. That fixed everything thank you again.
    – Kaane Guji
    Commented Feb 15, 2020 at 22:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.