使用computed计算属性,完成根据不同状态切换按钮

需求:我的订单组件接受到了来自不同状态的订单列表,我的按钮需要根据不同的订单改变,

.当然上面的需求可以根据v-if else 来解决,但是这里使用computed来完成的化话就要方便很多
所以解决思路,使用computed创建对应按钮列表来实现
我的订货组件
orders.vue

<template>
	<!-- 后续可以根据组件化来实现状态不同,数据也不相同来展示 这里可以封装为组件-->
	<view class="order-main-item" v-for="(item,index) in OrderList" :key='index'>
	<!-- 商品信息 -->
	<view class="order-info">
		<!-- 商品状态 -->
	<view class="info-status">
	  <!-- 店家信息 -->
	  <view class="info-status-shopper">
	  	<image :src="item.shopperAvater" mode="" class="shopper-img"></image>
		<!-- 店名 -->
		<view class="shopper-name">
			{{item.shopperName}}
		</view>
		<view class="iconfont icon-xiaoxizhongxin f-color">
			
		</view>
	  </view>
	  <!-- 支付状态 -->
	  <view class="info-status-sta  f-color-active">
	  	{{item.status}}
	  </view>
	</view>	
	<!-- 订单的具体内容 -->
	<view class="info-content">
		<image :src="item.goodImg" class="content-img"></image>
		<view class="content-info">
			<view class="info-text">
			{{item.goodName}}	
			</view>
			<view class="info-type f-color">
				{{item.type}}
			</view>
		</view>
		<!-- 价格和数量些 -->
		<view class="content-priceAndNum">
			<view class="content-price">
				¥{{item.total}}
			</view>
			<view class="content-num">
				*{{item.num}}
			</view>
			<view class="content-promise">
			{{item.promise}}
			</view>
		</view>
	</view>
	
	</view>
	<!-- 金额 -->
	<view class="order-pay">
		订单金额:¥<text class="f-color">{{item.total}}</text>(包含运费)
		
	</view>
	<line></line>
	<!-- 支付按钮 -->
	<view class="order-bt">
		<view class="bt-cancel f-color"  v-show="item.status==='待支付'">
			取消订单
		</view>
	<view class="bt-confirm f-color-active"  @click="btactive(item.status,item.id)">
		{{btActions[index]}}
	</view>
		
	</view>	
	</view>
	
	
	
	<!-- 如果没有数据显示  该区域-->
	<div class="when-Nodata" v-show="OrderList.length==0">
		<view class="nodata-div">
			您还没有相关订单
			<view class="Nodata-button" @click="goIndex()">
				去首页逛逛
			</view>
		</view>
		       
		
	</div>
</template>

js

<script setup>
	import {computed, defineProps, reactive, ref} from 'vue'
	import{onLoad} from '@dcloudio/uni-app'
const props=	defineProps({
	data:{
	  type:Array,
	  required:true
	}
});

const OrderList=props.data;
//计算属性真的重要
const btactive=(s,id)=>{
	//通过传递的不同status和id来完成对应执行逻辑
};
onLoad(()=>{
	console.log("初始化接受数据,",props.data)
	
});
    // 根据单个订单状态来获取按钮文本
    const getButtonAction = (status) => {
        switch(status) {
            case '待支付':
                return '支付';
            case '待收货':
                return '查看取件码'; // 或者你可能想要返回'已支付'或其他文本
            case '待发货':
                return '催一下';
            // 添加其他状态
            default:
                return '去评论';  // 完成订单
        }
    };

    // 为每个订单单独计算按钮文本
    var btActions = computed(() => {
        if (OrderList.length === 0) return [];
        return OrderList.map(order => getButtonAction(order.status));
    });



const goIndex=()=>{
	uni.showLoading({
		success() {
				uni.switchTab({
					url:'/pages/index/index'
				})
		},
		title:'努力加载中'
	})
	setTimeout(()=>{
	uni.hideLoading();		
	},1500)

	
}
</script>

根据计算属性,计算出一个对应的按钮列表,并且使用数组的map方法进行替换,这样按钮数组和数据数组就是同步展示,并且事件绑定也是根据传递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,针对您的问题,可以参考如下思路: 1. 在 Vue ,我们可以使用 `v-for` 指令来遍历数组,生成对应的元素。 2. 对于按钮的点击事件,我们可以使用 `@click` 来监听,并在对应的方法修改响应式数据,从而实现样式的切换。 综上,我们可以编写如下代码来实现点击按钮切换不同的字体颜色和字体大小: ```html <template> <div> <p v-for="(style, index) in styles" :key="index" :class="style.className">{{ text }}</p> <div> <button v-for="(style, index) in styles" :key="index" @click="setActive(index)">{{ style.name }}</button> </div> </div> </template> <script> export default { data() { return { text: 'Hello, World!', styles: [ { name: '默认样式', className: 'text1' }, { name: '红色字体', className: 'text2' }, { name: '大号字体', className: 'text3' }, ], activeIndex: 0, }; }, methods: { setActive(index) { this.activeIndex = index; }, }, computed: { activeStyle() { return this.styles[this.activeIndex]; }, }, }; </script> <style> p { font-size: 16px; } .text1 { color: black; } .text2 { color: red; } .text3 { font-size: 24px; } </style> ``` 在上述代码,我们使用了 `v-for` 指令来遍历 `styles` 数组,生成对应的 `<p>` 元素和按钮。我们为每个 `<p>` 元素绑定了对应的类名,这些类名由 `styles` 数组的对象的 `className` 属性决定。我们为这些类名分别定义了不同的样式,例如 `text1` 对应黑色字体,`text2` 对应红色字体,`text3` 对应更大的字号。在需要时,我们通过修改 `activeIndex` 的值来切换不同的样式,从而实现不同的样式效果。我们为每个按钮绑定 `@click` 事件,并在对应的方法修改 `activeIndex` 的值,从而实现样式的切换。最后,我们使用了一个计算属性 `activeStyle`,根据 `activeIndex` 的值返回当前应该使用的样式对象,以便在模板动态绑定类名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝胖子不是胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值