[Vuejs]-V-for doesn't get his options on mounted

2👍

Try to JSON.parse() your response:

const app = Vue.createApp({
  data() {
    return {
      listedespartenaires : [],
      selected: null
    };
  },
  methods:{            
    setNoms(lesnoms){
      this.listedespartenaires = JSON.parse(lesnoms);
      //alert(lesnoms);
    }
  },
  mounted() {
    fetch("https://jsonplaceholder.typicode.com/users")
      .then((response) => response.text())
      .then((data) => this.setNoms(data));
  }
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <select id="listenomspartenaires" v-model="selected">
    <option v-for="partenaire in listedespartenaires" :value="partenaire.id">
      {{ partenaire.name }}
    </option>
  </select>
  {{ selected }}
</div>

Leave a comment