Vue3 中使用计算属性获取额外信息并赋值
在 Vue3 中,可以使用计算属性来实现在保持现有代码不变的情况下额外获取'item.docSign'并赋值给'planModel.docSignSymptom'。
首先,需要在 Vue 组件的'computed'选项中定义一个计算属性,用于获取'item.docSign'的值并赋给'planModel.docSignSymptom'。代码如下:
computed: {
doctorOption() {
return (input, option) => {
const docSign = option.data.item.docSign;
this.planModel.docSignSymptom = docSign;
return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
};
}
}
在上述代码中,我们定义了一个计算属性'doctorOption',该计算属性返回一个函数作为'filter-option'属性的值。这个函数接收两个参数:'input'和'option'。我们在这个函数中获取'item.docSign'的值,并将其赋给'planModel.docSignSymptom'。
然后,将计算属性'doctorOption'作为'filter-option'属性的值传递给'a-select'组件。
完整的代码如下:
<a-select v-model:value='planModel.docNameSymptom' show-search :filter-option='doctorOption' style='width: 300px'>
<a-select-option v-for='item in doctors' :key='item.docPinyin' :value='item.docName'>
{{ item.docName }}
</a-select-option>
</a-select>
export default {
data() {
return {
planModel: {
docNameSymptom: '',
docSignSymptom: ''
},
doctors: [
{ docName: 'Doctor 1', docPinyin: 'doctor1', docSign: 'Sign 1' },
{ docName: 'Doctor 2', docPinyin: 'doctor2', docSign: 'Sign 2' },
{ docName: 'Doctor 3', docPinyin: 'doctor3', docSign: 'Sign 3' },
]
};
},
computed: {
doctorOption() {
return (input, option) => {
const docSign = option.data.item.docSign;
this.planModel.docSignSymptom = docSign;
return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
};
}
}
};
在上述代码中,我们使用了一个示例数据数组'doctors',其中包含了医生的姓名('docName')、拼音('docPinyin')和签名('docSign')。根据你的实际需求,你需要替换这个示例数据数组为你自己的数据。
这样,当选择一个医生时,'planModel.docSignSymptom'会被自动赋值为对应医生的签名。
原文地址: https://www.cveoy.top/t/topic/p69i 著作权归作者所有。请勿转载和采集!