【问题标题】:Vue "v-for" only showing last iteration (in v-select)Vue“v-for”仅显示最后一次迭代(在 v-select 中)
【发布时间】:2019-07-30 01:34:37
【问题描述】:

我目前正在尝试使用数据和自定义项目文本填充我的 Vue 项目的 v-select。

我发现使用 v-for 在列表中有效,但在我的 v-select 模板中无效。

作品:

<ul id="example-1">
     <li v-for="item in appointments">
           ID: {{ item.id }} | {{ item.client_name }}
      </li>
 </ul>

显示:

- ID: 1 | John Smith
- ID: 2 | Carl Larson
- ID: 3 | Lennard Smith

然后是我的工作选择,它没有使用模板(有效):

<v-select
   v-model="form.lead_generator_id"
   prepend-icon="person"
   :items="appointments"
   label="Appointment Setter"
   item-value="id"
   item-text="client_name"
></v-select>

然后是我的模板 v-select:

<v-select
  v-model="form.lead_generator_id"
  prepend-icon="person"
  :items="appointments"
  label="Appointment Setter"
  item-value="id"
  item-text="client_name"
>
    <template  slot="selection" slot-scope="appointments" v-for="item in appointments">
     ID: {{ item.id }} | {{ item.client_name }}
    </template>

    <template slot="item" slot-scope="appointments" v-for="item in appointments">
     ID: {{ item.id }} | {{ item.client_name }}
    </template>
</v-select>

最后一个的问题是它显示(在选择框中):

ID: 3 | Lennard Smith
ID: 3 | Lennard Smith
ID: 3 | Lennard Smith

我正在通过 axios 从我的 API 收集信息,然后 storign 位于 data() 中,它返回 appointments: [],

我的 api 返回:

{
   "status":"ok",
   "appointments":[
        { "id": 1, "client_name": x, etc.. }
        { "id": 2, "client_name": x, etc.. }
    ],
}

请帮助我了解如何让它正确显示。

【问题讨论】:

  • 这是使用Vuetify's v-select 还是其他一些组件?
  • 是的,它正在使用 v-select @Phil 。我发现不使用 v-select 时使用选项确实有效。

标签: javascript vue.js vuetify.js


【解决方案1】:

假设这是Vuetify's v-select component,我认为您误解了作用域插槽。

itemselection 插槽通过列表中的单个项目传递,您无需使用 v-for 迭代任何内容。

暂时忽略这一点,如果你只是想自定义显示的文本,你可以创建一个简单的函数来渲染文本并将其绑定到item-text,比如

<v-select :item-text="item => `ID: ${item.id} | ${item.client_name}`" ...

演示...

new Vue({
  el: '#app',
  data: {
    "appointments": [{
      "id": 1,
      "client_name": 'John Smith'
    }, {
      "id": 2,
      "client_name": 'Carl Larson'
    }, {
      "id": 3,
      "client_name": 'Lennard Smith'
    }],
    form: {
      lead_generator_id: null
    }
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet"><link href="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/vue"></script><script src="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.js"></script>
<div id="app">
  <v-app>
    <v-content>
      <v-select v-model="form.lead_generator_id" 
                prepend-icon="person" :items="appointments" 
                label="Appointment Setter" item-value="id" 
                :item-text="item => `ID: ${item.id} | ${item.client_name}`">
      </v-select>
      <pre>form = {{ form }}</pre>
    </v-content>
  </v-app>
</div>

【讨论】:

  • 非常感谢!我想我从 Vutify 文档跳到 Vue 让自己感到困惑。我需要更多地研究渲染函数,因为我以前并没有真正了解过它们。这正是我需要的:) @Phil
  • 这只是一个函数。 Vuetify 文档将可能的 item-text 属性值指定为 "string | array | function" (尽管我不知道它会对数组做什么)
  • 我现在明白了,这一切都开始变得有意义了。几个小时以来,我一直在尝试解决这个问题,很高兴它相对简单。
【解决方案2】:

为什么在模板中使用 for 循环?正确的做法是给 v-select 模板,范围必须是“选项” - 正如文档所说。

<template slot="option" slot-scope="option">
   ID: {{ item.id }} | {{ item.client_name }}
</template>

试试这个 https://codepen.io/anon/pen/RdVRRz

【讨论】:

  • 在文档中看不到 option 插槽。能给个链接吗?
  • 我发现与 v-for 的配对选项完全符合我的要求,除了我使用的是 Vuetify 并且当我将 select 更改为 v-select 时它会中断并说没有可用的数据 @Duong Dang。跨度>
  • @TimRowley 您使用的是哪个组件?给我github上的链接
  • @DuongDang OP 正在使用 Vuetify
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2018-11-08
相关资源
最近更新 更多