需求背景
需要实现一个复用性,是提供表单顺倒排序的按钮文章来源:https://www.toymoban.com/news/detail-608420.html
解决效果
文章来源地址https://www.toymoban.com/news/detail-608420.html
index.vue
<!--/**
* @author: liuk
* @date: 2023/7/25
* @describe: 排序按钮
*/-->
<template>
<div class="sort-fn">
<span :class="['positive',sortVal==='asc'?'select':'']" @click="toggleSort('asc')"></span>
<span :class="['reverse',sortVal==='desc'?'select':'']" @click="toggleSort('desc')"></span>
</div>
</template>
<script setup lang="ts">
import {computed} from "vue";
// Props
const props = defineProps({
modelValue: {
type: String || undefined,
required: true,
},
});
// Emits
const emit = defineEmits<{
(e: "update:modelValue", value: string | undefined): void
(e:"change"):void
}>()
// 父子组件数据双向绑定
const sortVal = computed<string | undefined>({
get() {
return props.modelValue;
},
set(val) {
// imgUrl改变时触发修改父组件绑定的v-model的值
emit('update:modelValue', val);
},
});
const toggleSort = (type) => {
sortVal.value = type;
emit('change');
}
</script>
<style lang="scss" scoped>
.sort-fn {
position: relative;
top: -5px;
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
width: 20px;
& > * {
width: 0;
height: 0;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
}
.positive {
border-bottom-color: #aaaaaa;
margin-bottom: 10px;
&.select {
border-bottom-color: #e6a33e;
}
}
.reverse {
border-top-color: #aaaaaa;
&.select {
border-top-color: #e6a33e;
}
}
}
</style>
到了这里,关于vue3 实现排序按钮的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!