uni-app微信小程序爬坑-父组件给子组件传值并绑定到子组件:style以及图片路径问题

**

前言

uni-app开发小程序油时候兼容性真的是千奇百怪

今天自己封装了一个无信息占位组件,组件源代码如下:
第一个问题请看注释

<template>
	<view class="no-tips flex-c-c">
		<image :src="imgSrc" :style="[imgStyle]"></image>
		//这里的imgStyle传过来的是一个对象,如果是这样用:
		//<image :src="imgSrc" :style="imgStyle"></image>
		//H5生效,小程序不生效,查看调试器,渲染的是这样的:style=[object,Object],
		//套上数组括号[]就解决这个问题了
		<text>{{tipsWord}}</text>
	</view>
</template>
<script>
	export default{
		props:{
				imgStyle:{
					type:Object,
					defualt:{}
				},
				tipsWord:{
					type:String,
					defualt:'暂无数据'
				},
				imgSrc:{
					type:String,
					defualt:'../static/img/common/no_tips.png'
				}
				
		},
		data(){
			return{
				
				
			}
		},
	}
</script>

<style lang="less" scoped>
	.no-tips{
		height: 60vh;
		image{
			width:300rpx ;
			height: 300rpx;
		}
	}
</style>

组件引用并传值:

//我已经全局挂载了组件,无需引入
<noTips 
tipsWord='暂无收藏' 
:imgSrc="require('../../static/img/userCenter/icon_nocollect.png')" 
:imgStyle="{width:'280rpx',height:'232rpx'}"
></noTips>

第二个问题子组件获取图片路径问题
传入图片的途径在H5端和小程序端不一样;
微信小程序端是根据组件的的目录来,H5是根据引用组件的目录来
一开始我这样用导致了两端不兼容:

:imgSrc="'../../static/img/userCenter/icon_nocollect.png'" 
调试器查看路径:
h5:'../../static/img/common/no_tips.png'
微信小程序:'../static/img/common/no_tips.png'

导致了两端只有一端能显示
然后改成这样就行了:

:imgSrc="require('../../static/img/userCenter/icon_nocollect.png')" 

有时候觉得是坑,其实是我们没有好好去看官方文档;

以下是uni-app官方说法: uni-app官网说明:Class 与 Style 绑定
Class 与 Style 绑定
为节约性能,我们将 Class 与 Style 的表达式通过 compiler 硬编码到 uni-app 中,支持语法和转换效果如下:

class 支持的语法:

<view :class="{ active: isActive }">111</view>
<view class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">222</view>
<view class="static" :class="[activeClass, errorClass]">333</view>
<view class="static" v-bind:class="[isActive ? activeClass : '', errorClass]">444</view>
<view class="static" v-bind:class="[{ active: isActive }, errorClass]">555</view>

style 支持的语法:

<view v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">666</view>
<view v-bind:style="[{ color: activeColor, fontSize: fontSize + 'px' }]">777</view>

非H5端不支持 Vue官方文档:Class 与 Style 绑定 中的 classObject 和 styleObject 语法。

不支持示例:

<template>
    <view :class="[activeClass]" :style="[baseStyles,overridingStyles]"></view>
</template>

<script>
    export default {
        data() {
            return {
                activeClass: {
                    'active': true,
                    'text-danger': false
                },
                baseStyles: {
                    color: 'green',
                    fontSize: '30px'
                },
                overridingStyles: {
                    'font-weight': 'bold'
                }
            }
        }
    }
</script>

注意:以:style=""这样的方式设置px像素值,其值为实际像素,不会被编译器转换。

此外还可以用 computed 方法生成 class 或者 style 字符串,插入到页面中,举例说明:

<template>
    <!-- 支持 -->
    <view class="container" :class="computedClassStr"></view>
    <view class="container" :class="{active: isActive}"></view>

    <!-- 不支持 -->
    <view class="container" :class="computedClassObject"></view>
</template>
<script>
    export default {
        data () {
            return {
                isActive: true
            }
        },
        computed: {
            computedClassStr () {
                return this.isActive ? 'active' : ''
            },
            computedClassObject () {
                return { active: this.isActive }
            }
        }
    }
</script>

用在组件上

非H5端(非自定义组件编译模式)暂不支持在自定义组件上使用 Class 与 Style 绑定

以上!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
uni-app中,要实现微信小程序图片放大预览并带有图片描述并可左右切换,可以使用uni-app提供的组件和方法来实现。 首先,我们可以使用uni-app的image组件来展示图片,并通过设置mode属性为aspectFit,使图片按照原图的长宽比等比缩放并保持完整显示。 然后,为了实现图片放大预览并可左右切换,在点击图片时,我们可以使用uni-app的previewImage方法来打开一个全屏的图片预览,并传入一个包含图片链接数组的current参数,以及一个包含图片描述的urls参数。这样就可以在预览界面中显示图片描述,并且可通过左右滑动切换图片。 具体实现步骤如下: 1.在模板中,使用image组件展示图片,设置mode为aspectFit,并绑定点击事件。 2.在点击事件处理函数中,调用uni-app的previewImage方法,传入图片链接数组和图片描述数组。 3.在预览界面中,通过swiper组件实现图片的左右滑动切换,并显示图片描述。 示例代码如下: ``` <template> <view> <image mode="aspectFit" :src="imageUrl" @click="previewImage"></image> </view> </template> <script> export default { data() { return { imageUrl: '图片链接', imageDesc: '图片描述' } }, methods: { previewImage() { uni.previewImage({ current: this.imageUrl, urls: [this.imageUrl], longPressActions: { itemList: ['保存图片'], success: function(data) { if (data.tapIndex === 0) { uni.saveImageToPhotosAlbum({ filePath: this.imageUrl, success: function() { uni.showToast({ title: '保存图片成功' }) }, fail: function() { uni.showToast({ title: '保存图片失败', icon: 'none' }) } }) } } } }) } } } </script> ``` 使用以上方法,我们可以在uni-app微信小程序中实现图片放大预览并带有图片描述可左右切换的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黒客与画家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值