记uniapp总动态:style无法用带参数的computed属性问题

问题描述:

v-for里面的组件呢,我要根据不同的item配置不同的style,于是有了这样的写法

<template>
    <view class="weeks-item" v-for="(day,dayIndex) in item" :key="dayIndex">
					
		<view :style="bgStyle(day)" >
			{{ day. extraInfo.info}}
		</view>
	</view>
</template>
<script>
    export default{
        computed:{
            bgStyle:function(){
				return (day)=>{
					if(day.extraInfo){
						return `background-color:${day.extraInfo.data.bgColor}`
					}else{
						return ''
					}
				}
			}}
    }
</script>

然后一运行 ,就给报错

👉🏻 ☞ 解决办法:
<template>
    <view class="uni-calendar__weeks-item" 
        v-for="(day,dayIndex) in styledItems(item)" :key="dayIndex">
						
	    <view class="daylayer-custom">
		    <view :style="day.dynamicStyle" >
			    {{ day. extraInfo.info}}
		    </view>
	    </view>
    </view>
</template>
<script>
    export default{
        computed:{
            styledItems:function(){
				return (item)=>{
					return item.map(day => ({
						...day,
						dynamicStyle: this.getDynamicStyle(day)
					}));
				}
			}
        },
        methods: {
            getDynamicStyle(day) {
				if(day.extraInfo){
						return `background-color:${day.extraInfo.data.bgColor}`
						
					}
				return ''
			},

        }
    }
</script>

这样,就可以了,主要修改思路就是把style作为day元素的一个属性,然后动态style绑定这个属性即可。

小感悟:写代码不要太固执,打不过就加入,主打一个曲线救国。

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,你的问题是关于uniapp在上传图片时报错"chooseAndUploadFile:fail api scope is not declared in the privacy agreement"。根据引用中的内容,该错误可能与用户隐私协议中声明的权限不一致有关。在使用uniapp上传图片时,需要确保已经在隐私协议中声明了相应的权限。你可以检查一下你的隐私协议并确保已经声明了相应的权限。 此外,根据引用中的内容,你还可以尝试解决方案来解决这个问题。可能是因为在代码中没有正确声明"to_string"函数导致了该错误。你可以在代码中添加#include <string>来引入相应的头文件,以确保该函数在代码中被正确声明。 综上所述,解决uniapp上传图片报错"chooseAndUploadFile:fail api scope is not declared in the privacy agreement"的方法是检查隐私协议中是否声明了相应的权限,并在代码中正确声明相关函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [codeblocks中报错:'to_string' was not declared in this scope解决方案](https://download.csdn.net/download/u013271326/10295792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Privacy Policy](https://blog.csdn.net/darling0825/article/details/84726637)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值