Skip to main content
  1. About
  2. For Teams
Asked
Viewed 506 times
0

I am attempting to iterate through an array from a Vue component without reapeating the element on which I call 'v-for'. I have not found a subsitute for 'v-for' in the official docs API, nor in articles online.

I have this:

<div v-for="(item, index) in items">
  <foo :index="index">
  <foo/>
</div>

Want this:

<foo :index="0"><foo/> 

<foo :index="1"><foo/>

<foo :index="2"><foo/>
//etc...

And have tried this, which does not work:

<foo v-for="(item, index) in items":index="index">
<foo/>

Help is much appreciated! Started coding with Vue.js yesterday.

3 Answers 3

1

Your HTML is wrong. You can do this just fine.

<foo v-for="(item, index) in items" :index="index"></foo>
Sign up to request clarification or add additional context in comments.

Comments

0
<foo v-for="(item, index) in items">
    {{index}}
</foo>

1 Comment

I have edited the question. I need to be able to use index in a foo attribute.
0

Does this work for you http://jsbin.com/kapipezalu/edit?html,js,console,output

You probably forgot to define props into foo component.

  <div id="app">
    <foo v-for="(item, index) in items" :index="index"></foo>
  </div>

  <template id="foo-temp">
    <div>
      <span>{{ index }}</span>
    </div>
  </template>

JS:

Vue.component('foo', {
  props: ['index'],
  template: '#foo-temp'
})

new Vue({

  el: '#app',

  data: {
    items: [
      {id: 1, title: 'One'},
      {id: 2, title: 'Two'},
      {id: 3, title: 'Three'}
    ]
  }

})

Pro Tip: Use Browser Console - It will show you some cool warnings and errors.

Comments

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.