【问题标题】:Vue : How to getting response data into select option (vue-search-select) with axiosVue:如何使用 axios 将响应数据放入选择选项(vue-search-select)
【发布时间】:2020-12-13 17:07:16
【问题描述】:

我是新手。我想使用 vue-search-select 在 select 选项上显示来自 axios 的响应数据,我已经尝试了几次但结果为零。我试过用卡循环数据,数据显示成功。但在这里我无法在选择选项上显示数据。

这是我的数据:

结果如图:

这是我的选择选项代码:

<model-select :options="hospitals"
                    option-value="value"
                    option-text="text"
                    v-model="item"
                    placeholder="Choose Hospital">
</model-select>

这是我的脚本:

import axios from "axios"
import { ModelSelect } from 'vue-search-select'

export default {
    data() {
        return {
            hospitals:[],
            item : '',
            // options: [
            //     { value: '1', text: 'aa' + ' - ' + '1' },
            //     { value: '2', text: 'ab' + ' - ' + '2' },
            //     { value: '3', text: 'bc' + ' - ' + '3' },
            //     { value: '4', text: 'cd' + ' - ' + '4' },
            //     { value: '5', text: 'de' + ' - ' + '5' }
            // ],
            // item: {
            //     value: '',
            //     text: ''
            // },
        }
    },
    mounted() {
        this.getHospitals();
    },
    methods: {
        getHospitals() {
            axios
                .get('http://127.0.0.1:8000/my-endpoint')
                .then(response => {
                    this.hospitals = response.data.data
                })
                .catch(err => {
                    console.log(err)
                })
        }
    },
    components: {
      ModelSelect
    }
}

谢谢。

【问题讨论】:

    标签: vue.js bootstrap-4 axios vue-cli


    【解决方案1】:

    改用 model-list-select

    应该是这样的

    <model-list-select :list="hospitals"
                        option-value="id"
                        option-text="hospital_name"
                        v-model="item"
                        placeholder="Choose Hospital">
    </model-list-select>
    

    并像这样更新您导入的组件

    import { ModelListSelect } from 'vue-search-select'
    

    【讨论】:

    • 导入的组件也需要更改。 import { ModelListSelect } from 'vue-search-select'
    【解决方案2】:
    <model-select :options="hospitals"
                        option-value="id"
                        option-text="hospital_name"
                        v-model="item"
                        placeholder="Choose Hospital">
    </model-select>
    

    你必须指定你需要的值键和文本键。

    【讨论】:

    • 我试过了,结果还是空白,和上图一样
    • 尝试使用 this.hospitals = response.data
    • @KuldipShelke 在我尝试之后,结果就像选择没有选项
    • 你能检查页面并检查控制台错误以及从网络选项卡返回的 api 吗?您可能会在控制台消息中获得更好的主意。
    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2018-08-05
    • 2021-08-11
    • 2022-11-03
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多