Vue 中使用 computed 实现 switch case 语句
<template>
<div>
<p>选择一个数字:</p>
<select v-model="selectedNumber">
<option v-for="option in options" :value="option">{{ option }}</option>
</select>
<p>你选择的数字是:{{ selectedNumber }}</p>
<p>数字的类型是:{{ getNumberType }}</p>
</div>
</template>
<script>
export default {
data() {
return {
selectedNumber: '',
options: [1, 2, 3, 4, 5]
}
},
computed: {
getNumberType() {
switch (this.selectedNumber) {
case 1:
return '奇数';
case 2:
return '偶数';
case 3:
return '奇数';
case 4:
return '偶数';
case 5:
return '奇数';
default:
return '未选择数字';
}
}
},
methods: {
// 其他方法
}
}
</script>
<p>在上面的代码中,使用了 computed 属性来实现 switch case 语句的功能。根据用户选择的数字,返回对应的数字类型(奇数或偶数)。然后可以在模板中使用{{ getNumberType }}来显示数字类型。如果用户没有选择数字,则返回'未选择数字'。</p>
原文地址: https://www.cveoy.top/t/topic/mkeF 著作权归作者所有。请勿转载和采集!