0

I am trying to use an inline-template in Vue.js but I can't get it to display data, here is my code:

Vue.component('food-item', {

    props: ['food'],


})


var app7 = new Vue({
    el: '#app-7',
    data: {
        foodList: [
            { id: 0, text: 'Vegetables' },
            { id: 1, text: 'Cheese' },
            { id: 2, text: 'Whatever else humans are supposed to eat' }
        ]
    }
})

HTML Code:

<div id="app-7">
<ol>
    <food-item v-for="item in foodList" v-bind:food="item" v-bind:key="item.address">
    </food-item>
</ol>

<!-- template -->
<food-item inline-template>
    <li>{{ food.text }}</li>
</food-item>
</div>

I get the following error:

TypeError: Cannot read property 'text' of undefined

1 Answer 1

4

You're treating it as a way of specifying the template separate from the component. That's not how inline-template works. The inline-template attribute and the template specification go in the tags where you're instantiating the component.

Vue.component('food-item', {
  props: ['food'],
})


var app7 = new Vue({
  el: '#app-7',
  data: {
    foodList: [{
        id: 0,
        text: 'Vegetables'
      },
      {
        id: 1,
        text: 'Cheese'
      },
      {
        id: 2,
        text: 'Whatever else humans are supposed to eat'
      }
    ]
  }
})
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<div id="app-7">
  <ol>
    <food-item v-for="item in foodList" v-bind:food="item" v-bind:key="item.address" inline-template>
      <li>{{ food.text }}</li>
    </food-item>
  </ol>
</div>

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

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.