在 Vue 2 和 Vue 3 的上下文中,Element UI 和 Element Plus(Element UI 的 Vue 3 版本)都提供了 el-rate
评分组件。然而,由于 Vue 3 和 Element Plus 的出现,API 和使用方式可能会有所不同。以下是对 Vue 2 的 Element UI 和 Vue 3 的 Element Plus 中的 el-rate
组件的详细比较和示例。
Vue 2 + Element UI
el-rate 组件
属性 (Attributes):
value
: 绑定值,当前选中的评分max
: 允许的最大值,默认为 5allow-half
: 是否允许半选low-threshold
: 低分和中等分数的界限值,且值需要小于等于max
high-threshold
: 高分和中等分数的界限值,且值需要小于等于max
colors
: 自定义颜色列表,默认为['#F7BA2A', '#F7BA2A', '#F7BA2A', '#F7BA2A', '#F7BA2A']
void-color
: 未选中时的颜色disabled
: 是否禁用show-text
: 是否显示辅助文字,默认为false
show-score
: 是否在右侧显示当前分数,默认为false
text-template
: 自定义分数文本的模板,如{value}
代表分数...
: 其他通用属性
事件 (Events):
change
: 绑定值变化时触发...
: 其他通用事件
方法 (Methods):
- Element UI 的
el-rate
组件通常不提供直接调用的方法,而是通过属性和事件来控制其行为。
示例:
<template>
<el-rate
v-model="value"
:max="5"
:allow-half="true"
:low-threshold="2"
:high-threshold="4"
@change="handleChange"
show-score
></el-rate>
</template>
<script>
export default {
data() {
return {
value: 0,
};
},
methods: {
handleChange(value) {
console.log('Rating changed: ', value);
},
},
};
</script>
Vue 3 + Element Plus
在 Vue 3 中,你将使用 Element Plus 的 el-rate
组件,其 API 大致相同,但可能会有一些差异或新增功能。你应该查阅 Element Plus 的官方文档以获取最新的信息。
示例:
在 Vue 3 中使用 Element Plus 的 el-rate
组件的示例代码与 Vue 2 类似,但你需要使用 Composition API 来定义响应式的数据和方法。
<template>
<!-- 模板部分与 Vue 2 示例类似 -->
<el-rate
v-model="rateValue"
:max="5"
:allow-half="true"
:low-threshold="2"
:high-threshold="4"
@change="onRateChange"
show-score
></el-rate>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const rateValue = ref(0);
const onRateChange = (value) => {
console.log('Rating changed: ', value);
};
return {
rateValue,
onRateChange,
};
},
};
</script>
请注意,以上示例代码是基于 Element UI 和 Element Plus 的常见 API 编写的,并假设了某些属性的默认值。在实际使用中,你应该根据项目的具体需求和库的版本查阅官方文档。