0

In my application there is a parent component that contains a list of objects that represents different fruits. When a user selects a fruit, its data are passed to the child component which displays all the data as details.

        <app-details 
            [fruit]="selectedFruit"
        ></app-details>

Inside the details template:

<div class="fruit-details">
    <h1>{{fruit.name}}</h1>
    <h1>{{fruit.color}}</h1>
    <h1>{{fruit.description}}</h1>
</div>

'fruit' property is set to type object in details component. @Input()fruit: Object;

An error occurs stating something like Property 'description' does not exist on type 'Object'. Not setting "fruit" property to datatype of "Object" solves the issues. But How can I solve this issue without removing the data type.

1
  • 3
    Object is not the right type for fruit. fruit: { [key: string]: any } would work better, and if you could create your own Fruit interface, that would be ideal. Commented Jun 10, 2020 at 3:18

3 Answers 3

1

Declare an interface for Fruit and then use that as your type:

interface Fruit { 
    name: string;
    color: string;
    description: string;
}

@Input()fruit: Fruit;
Sign up to request clarification or add additional context in comments.

Comments

0

how about using @Input()fruit: string, stringify your selectedFruit

JSON.stringify(selectedFruit)

in details component parse it :

let parseFruit = JSON.parse(this.fruit); 
console.log(parseFruit);

Comments

0

to my best knowledge and after hours of searching, all you need to do is to Declare any variable that you intend to use in your HTML at the class level of your component (where the export class definition at your type script)

I hope to hear it solved the problem to anyone Beni Dayan.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.