0

i am trying to pass some values from a json file to a component however i keep getting "product is undefined"

product.json

{
    "product": {
        "price": 1.11,
        "amount": 0.11
    }
}

component in vue js:

<div v-for="prod in product" :key="price">

How can i get rid of this error?

1
  • For using v-for you should use array. So the product should be an array itself. Commented Apr 18, 2020 at 4:48

2 Answers 2

1

If you defined product in the data of your Vue instance, you could use it within the v-for.

I've included a working version in the fiddle below.

let productJson = {
    "product": {
        "price": 1.11,
        "amount": 0.11
    }
};

new Vue({
  el: "#app",
  data: () => {
    return productJson;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="(value, key) in product">{{key}}: {{value}}</div>
</div>

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

Comments

1

I'm guessing you need to loop over the keys of the object.

You can use Object.keys for this.

<div v-for="prodKey in Objects.keys(products)" :key="products[prodKey].price">

Comments

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.