如何使用 axios 中的数据填充 Vuetify Select

Isa*_*edo 4 html-select vue.js axios vuetify.js

我需要填充一个 Vuetify select,但它有问题,我的 Get 方法返回数据,但 vuetify select 只显示如下内容: 在此处输入图片说明

文本显示有效数据:

[ { "id": 1 }, { "id": 2 } ]
Run Code Online (Sandbox Code Playgroud)

并填充 Select 我按照文档添加 :items="entidades" and :item-text="entidades.id" and :item-value="entidades.id"

<v-select :items="entidades" :item-text="entidades.id" :item-value="entidades.id" single-line auto prepend-icon="group_work" label="Seleccionar Grupo"></v-select>
Run Code Online (Sandbox Code Playgroud)

这是我的代码表单脚本

`data() {
return(){
entidades: [{
          id: ''  
        }],
}
}`
Run Code Online (Sandbox Code Playgroud)

我已经尝试输入 0,但结果相同。

我的 axios.get 方法。

    axios.get('http://localhost:58209/api/GetEntidades', {
      headers:{
       "Authorization": "Bearer "+localStorage.getItem('token')
          }
  })
    .then(response => { 
      console.log(response)
      this.entidades = response.data;
        })
        .catch(error => {
        console.log(error.response)
        });
Run Code Online (Sandbox Code Playgroud)

非常感谢

acd*_*ior 9

item-textitem-value分别是每个项目将显示和用作值的属性的名称。所以使用item-text="id" item-value="id"

<v-select :items="entidades" item-text="id" item-value="id" single-line auto prepend-icon="group_work" label="Seleccionar Grupo"></v-select>
Run Code Online (Sandbox Code Playgroud)

演示:

<v-select :items="entidades" item-text="id" item-value="id" single-line auto prepend-icon="group_work" label="Seleccionar Grupo"></v-select>
Run Code Online (Sandbox Code Playgroud)
new Vue({
  el: '#app',
  data () {
    return {
      entidades: [ { "id": 1 }, { "id": 2 } ]
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

  • 是的,没有`:`。当你使用 `:item-text="someValue"` 时,它会在当前作用域中寻找 `someValue` 变量。在您的情况下,您可能使用了 `:item-text="id"` 并且可能出现错误,因为当前范围内不应该有 `id` 变量(`id` 是每个 `entidade` 的属性,不是它自己的变量)。 (2认同)