2

So i have some object with posts, and i'm using v-for to iterate them in the custom component, but how to pass data from this object to this component, loop is a one thing displaying data i another...

<app-single-post v-for="post in posts" postData="$post"></app-single-post>

This is my component declaration. Do i need also some kind of special prop setup? Have the same error, again and again:

Property or method "postData" is not defined

2
  • You can pass data into a component via 'Props'. See vuejs.org/v2/guide/components.html#Passing-Data-with-Props for more details. Props should also be set as kebab case eg 'post-data' on the html element. Commented May 12, 2017 at 23:09
  • what you mean data vie props? can you show some example? Commented May 12, 2017 at 23:10

1 Answer 1

10

Use the binding syntax.

<app-single-post v-for="post in posts" :post="post" :key="post.id"></app-single-post>

Camel-cased properties need to be converted to kebab-case when they are used as attributes. Also, I added a key. You should always use a key when you use v-for and it is required when you iterate over a custom component. Ideally you would want to use a post.id if one is available.

In your component, you should have a property defined like this:

export default {
    props:["post"],
    methods: {...},
    etc.
}

To reference the post in your component template you can use

 {{post.id}}

and inside methods it would be

this.post
Sign up to request clarification or add additional context in comments.

8 Comments

ok, but how to get acces to the object in component by {{post.id}} i have undefined error :(
@Lukas in your component you need to define the property, props: ["postData"].
@lukas you want it called post in the component?
Are you sure that individual objects in posts have a key called id? If not, it will throw an undefined error.
@Terry I inferred it from his comment; but you're right if it's not there it'll be undefined.
|

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.