eli
eli

Reputation: 313

Property doesn't exist on component

I have this Angular 4 component :

export class MenuComponent {

constructor(private menuService: MenuService) { }


@Input(nodes):any;

   getMenu(path:string): void {
   this.menuService.getData(path).subscribe(data => {
         // Read the result field from the JSON response.

         let newValue = JSON.stringify(data).replace('{"Node":', '[');
         newValue = newValue.substring(0,newValue.length - 1);
         newValue+="]";
        const menu=JSON.parse(newValue);
         this.nodes = menu;
      });

 }
}

I keep getting this error: Property 'nodes' does not exist on type 'MenuComponent' and I have no idea why because the nodes property is right there.

Upvotes: 2

Views: 3973

Answers (2)

Madhu Ranjan
Madhu Ranjan

Reputation: 17944

you can provide Input in two ways,

 @Input() hero: Hero;
 @Input('master') masterName: string;

In your code you are providing public property name (bindingPropertyName) but there is no property which can be used in component class,

 @Input(nodes):any;

Read more about @Input here.

Upvotes: 1

Aniruddha Das
Aniruddha Das

Reputation: 21698

I normally define @input variables in this way.

  @Input() nodes: any;

I can see two problems

  1. from the view you your component getting undefined or not getting value. so make sure parent component is sending proper value [nodes]="'this is a value"'

  2. in angular component life cycle, make sure the value of nodes is coming on time.

You can also add ngInit life cycle hook and set a default value in case did not receive any thing

  ngOnInit() {
    this.nodes = this.nodes || '';
  }

Upvotes: 0

Related Questions