【问题标题】:How to put api data value into select options for quasar framework?如何将 api 数据值放入 quasar 框架的选择选项中?
【发布时间】:2021-01-22 08:34:12
【问题描述】:

我正在尝试将我的 api 值放入 q-select 中,但它显示所有值的 [object Object]

<q-select
   v-model="product_brand"
   :options="brands"
   label="Product Brand *"
   lazy-rules
   :rules="[ val => val && val.length > 0 || 'Please select product brand']"
/>

这是获取 api 值的脚本代码

export default () {
  data () {
    return {
      brands: []
    }
  },
  created () {
   this.$axios.get(['http://127.0.0.1:8000/api/lailen-inventory/categories'])
    .then(res => {
      this.brands = res.data.categories
    })
    .catch(errors => {
      console.log(errors)
    })
  },
}

【问题讨论】:

    标签: javascript vue.js axios quasar-framework quasar


    【解决方案1】:

    例如,如果品牌数组是这样的

    brands = [{label : "A", id : 1},{label : "B", id : 2}] 
    <q-select
       v-model="product_brand"
       :options="brands"
       option-label="label"
       option-value="id"
       label="Product Brand *"
       lazy-rules
       :rules="[ val => val && val.length > 0 || 'Please select product brand']"
    />
    

    如果您只想收集值而不是对象,请使用 emit-value 和 map-option 道具,您的代码将如下所示:

    <q-select
       v-model="product_brand"
       :options="brands"
       option-label="label"
       option-value="id"
       label="Product Brand *"
       lazy-rules
       emit-value
       map-options
       :rules="[ val => val && val.length > 0 || 'Please select product brand']"
    />
    

    【讨论】:

    • 它正在正确获取值,但在选项显示上它仍然显示 [object Object] @waqar
    • 向我展示他们的乐队数组,以便我可以更好地告诉你
    • 可能您仍在使用 option-label="label" option-value="id" 使用品牌数组对象中存在的键 option-label="{display-key}" option- value="{value-id}"
    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 2016-07-08
    • 2020-03-03
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多