2

I am very new in Vue and I am trying to loop through an array. Don't exactly know what I am doing wrong but the list is not displaying on HTML. Here is the code below: This is an index file that is being rendered through a router view.

<template>
  <div class="index container">
    <div class="card" v-for="tournament in tournaments" :key="tournament.id">
      <div class="card-content">
        <h2 class="indigo-text">{{tournament.title}}</h2>
        <ul class="tournaments">
          <li v-for="(score,index) in tournamnet.scores" :key="index"></li>
          <span class="chip">{{score}}</span>
        </ul>
      </div>
    </div>
    
  </div>
</template>

<script>
export default {
  name: 'index',
  data () {
    return {
     tournaments:[
       {title:'Muthaiga golf Tournament',slug: 'muthaiga-golf-tournament',scores:['Round 1', 'Round 2', 'Round 3'],id:'1'},
       {title:'Wilson Churchhill',slug: 'Wilson Churchhill',scores:['Round 1', 'Round 2', 'Round 3'],id:'2'},
       ]
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>

</style>

Here is the router view index.js

import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index
    }
  ]
})

And here is the app.vue

<template>
  <div id="app">
   <navbar />
    <router-view/>
  </div>
</template>

<script>
import navbar from '@/components/navbar'
export default {
  name: 'App',
  components:{
    navbar
  }
}
</script>

Any help will be highly appreciated.

4
  • What do you see? Do you see your navbar? What about the <div class="index container">? What is it exactly that's not working as expected? Commented Aug 4, 2020 at 23:24
  • Does this answer your question? Correct way to handle v-if with v-for in Vue Commented Aug 5, 2020 at 3:42
  • I can see my NavBar but I cant see the <div class="index container"> Commented Aug 5, 2020 at 5:34
  • 1
    are you sure this is not because of the typo tournamnet.scores which you wrote instead of tournament.scores? Commented Aug 5, 2020 at 14:40

2 Answers 2

3

Place your span inside the v-for

<ul class="tournaments">
  <li v-for="(score,index) in tournament.scores" :key="index+'tournament'">
    <span class="chip">{{score}}</span>
  </li>
</ul>

Also, it is better not to use the index as a key, I added string 'tournament' to make it more unique.

Additionally, please make sure you got the spelling corrected for 'tournament'.


Link to official docs.

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

3 Comments

What's wrong with index as the key? FYI, I agree that using array indices as keys is often not a good idea (especially if the array gets sorted) but there's nothing wrong with it and they don't need to be made more unique
Oh, that is just to avoid problems in case if he had another v-for with index as key somewhere in the component for example. Sometimes it also gives a warning in the console, depending on the setup.
You make an excellent point but in that case, you'd be better renaming the index variable itself
1

You have a typo on <li v-for="(score,index) in tournamnet.scores" :key="index"></li>

Mispelled tournament

If you look in the console you should see

vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "score" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Index> at src/components/index.vue
       <App> at src/App.vue
         <Root>

1 Comment

Thank you so much, the typo was the problem. I can now see my titles, but not the scores. Let me sift through again to see if I have another typo causing me agony.

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.