[Vuejs]-Dynamic data doesn't update when changing v-model input value in Vuejs

0👍

Your code working correctly. Type a number to see the name. You need to set the results.

new Vue({
  el: '#app',
  data() {
    return {
      results: [],
      pokemonName: ""
    }
    this.$set(this.pokemonName)
  },
  methods: {
    getPokemon() {
      // await axios
      fetch('https://pokeapi.co/api/v2/pokemon/' + this.pokemonName)
        //.then(response => (this.results = response.data));
        .then(response => response.json()).then(data => {
          this.results = data.name;
        })
        .catch(function(error) {
          console.log(ERROR);
        })
    }
  },
  mounted: function mounted() {
    this.getPokemon()
  }
});
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Pokemon Search</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="style.css">
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>

  <div id="app" class="container">

    <div>{{ results }}</div><br /> Search: <input type="text" v-model.lazy="pokemonName" @change="getPokemon">
  </div>

</body>

</html>

Leave a comment