1

I'm learning how to use Vue and one of the methods in my practice code isn't working, any idea why?

When clicking 'Add name' an alert should pop up, but it doesn't.

new Vue({
  el: '#array',
  data: {
    names: ['Jo', 'Joana', 'Joanna', 'Joan']
  },
   
methods: {
  addName: function() {
    alert('Adding name');
  }
}

 });
<script src="https://unpkg.com/vue"></script>

<div id="array">
  <ul>
    <li v-for="name in names" v-text="name"> {{ names }} </li>
  </ul>
</div>

<input type="text">
<button v-on:click="addName">Add name</button>

2
  • 1
    The button is not inside the Vue. Commented Sep 2, 2017 at 5:58
  • yeah, this is from the LaraCast and I had the same problem Commented May 5, 2018 at 20:16

1 Answer 1

2

Try this.

new Vue({
  el: '#array',
  data: {
    names: ['Jo', 'Joana', 'Joanna', 'Joan'],
    newName: ""
  },
  methods: {
    addName: function() {
      this.names.push(this.newName);
      this.newName = ""
    }
  }
});
<script src="https://unpkg.com/vue"></script>

<div id="array">
  <ul>
    <li v-for="name in names"> {{ name }} </li>
  </ul>
  <input v-model="newName" type="text">
  <button v-on:click="addName">Add name</button>
</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.