javascript - Vuejs 从 api 填充选择选项
问题描述
我是 VueJs 的初学者,所以我有这个问题
这是我的代码
<md-select placeholder="Seleccione Lote" v-model="selectedLote" name="lotes" id="lotes">
<md-option v-for="lote of lotes" value="lote.value">{{lote.text}}</md-option>
</md-select>
然后我有我的数组
data: () => ({
lotes: [],
selectedLote : null
})
然后我使用这个填充这个数组(this.products 是一个从 api 请求填充的数组)
mounted(){
this.fillLotesArray();
},
methods:{
fillLotesArray(){
for(let product of this.products){
if(this.lotes.findIndex(lote => lote.value === product._source.Lote) === -1){
this.lotes.push({value:product._source.Lote, text:product._source.Lote});
}
}
}
}
使用 console.logs 我可以看到数组已正确填充,但选项没有
解决方案
这通常是我从 API 编写选择的标准方式
<template>
<select v-model="selectedOption">
<option v-for="(item, index) in options" :value="item.value" :key="index">{{ item.text }}</option>
</select>
</template>
<script>
export default {
name: 'ExampleOptions',
data() {
return {
options: [],
selectedOption: null,
};
},
mounted() {
this.loadOptions();
},
methods: {
loadOptions() {
api.getOptions().then((options) => {
this.options = options;
});
},
},
};
</script>
如果您随后需要将数据与其他数据相关联,例如产品列表或其他数据,则始终建议使用计算属性,以确保数据始终正确 一些示例
推荐阅读
- python - 如何将 Plotly dash 应用程序放在受保护的路线后面
- python-3.x - 在 IBM WATSON STUDIO 中将脚本导入笔记本
- chart.js - 向下钻取 chart.js 事件点击功能
- testing - TestCafe 有设置默认浏览器窗口大小的方法吗?
- google-cloud-platform - 数据流作业消耗未结束,系统延迟长时间增长
- android - 使用自定义 ArrayAdapter 的菱形运算符
- php - 三重等于返回php
- android - Android:TextInputLayout - 自定义提示、底线和错误消息的颜色
- unity3d - Unity 自定义编辑器 - 基于派生类型添加数据字段
- jquery - jquery添加/删除类后恢复正常状态