要实现接口传值时数字页面上显示的是文字,可以通过以下步骤进行转换:

  1. 在页面加载时,通过接口获取字典项列表数据,并保存在一个字典对象中,例如:
// 假设接口返回的字典项列表数据为:
// [{ value: 1, label: '选项1' }, { value: 2, label: '选项2' }, ...]
const dictionary = {};

// 获取字典项数据
getDictionaryData().then((data) => {
  data.forEach((item) => {
    dictionary[item.value] = item.label;
  });
});

这样可以将数字值作为字典对象的键,文字作为对应的值。

  1. 在下拉框组件中,将接口返回的数字值转换为文字显示。可以通过计算属性来实现,在模板中使用计算属性的值来显示转换后的文字,例如:
data() {
  return {
    // 接口返回的原始值
    selectedValue: 1
  };
},
computed: {
  // 将数字值转换为文字显示
  selectedLabel() {
    return dictionary[this.selectedValue];
  }
}

在模板中,使用selectedLabel来显示转换后的文字:

<template>
  <div>
    <select v-model="selectedValue">
      <option v-for="item in dictionary" :value="item.value" :key="item.value">{{ item.label }}</option>
    </select>
    <p>选中的文字:{{ selectedLabel }}</p>
  </div>
</template>
  1. 在提交表单或调用接口时,根据显示的文字值获取对应的数字值进行传参。可以通过字典对象的键值对进行反向查找,找到对应的数字值,例如:
// 提交表单或调用接口时获取选中的数字值
const selectedValue = Object.keys(dictionary).find((key) => dictionary[key] === this.selectedLabel);

这样可以确保传参时是数字值。

通过以上步骤,可以实现接口传值时数字页面上显示的是文字,并在传参时保证传输的是数字

avue下拉框字典项页面页面接口传值时数字页面上显示的也是数字如何做转换保证接口传参时显示的是文字传输的是数字

原文地址: https://www.cveoy.top/t/topic/ilMh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录