0

I'm trying to get just a simple hello world for v-for. I've googled this for an hour and found various other posts, such as this one to no use.

My HTML:

<ul>
    <li v-for="item in history">
        {{ item }}
    </li>
</ul>

My JS:

new Vue({
    el: '#app',
    data: {
        history: [
            'red','green','blue'
        ],
    },
});

My Output: {{ item }}

Screenshot of output

Someone please tell me what I'm doing wrong and why I can't get this bare-bones example working. I've tried using arrays and objects, looping using the key attribute, and several other things.

Docs: https://v2.vuejs.org/v2/guide/list.html

EDIT: apologise for the awful title; I had to change it 13 times before SO stopped yelling at me...

EDIT 2: I have the app defined containing the entire body of HTML: <div id="app">, thank you for the answers though : )

EDIT 3: Resolved, I must've had a div tag closed prematurely; commenting it out fixed the code. Thanks for the answers.

1
  • I've got the CDN working correctly and the app set up otherwise, I just can't get v-for working. What's wrong with my code? Commented Aug 15, 2020 at 4:52

2 Answers 2

1

Probably you don't define the id app in the HTML section. Here is the working code.

new Vue({
  el: '#app',
  data: {
    history: [
      'red', 'green', 'blue'
    ],
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<ul id='app'>
  <li v-for="item in history">
    {{ item }}
  </li>
</ul>

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

1 Comment

Thank you for the reply. I have the app defined like so: <div id="app">, containing all other code including my HTML.
0

You need to have "#app" id in your code like this

<ul id="app">
    <li v-for="item in history">
        {{ item }}
    </li>
</ul>

1 Comment

Thank you for the reply. I have the app defined like so: <div id="app">, containing all other code including my HTML.

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.