pp电子

登录
免费开通

微信小程序之怎样使用自界说组件封装原生image组件

小程序原生组件image组件没有常用的功效,,,,,以是以下小编为各人介绍微信小程序之怎样使用自界说组件封装原生image组件 ??

微信小程序之怎样使用自界说组件封装原生image组件

目录
  • 1、通例操作
  • 2、自界说组件

通例操作

在小程序没还没推出自界说组件功效时,,,,,只能通过改变 Page 中的 data 来展示兜底的占位图,,,,,以是其时的处理方式十分蛋疼...

1.1.相同默认图

由于需要知道这个图片的数据路径,,,,,以是不得不在每个 image 上加上类似 data-img-path 的工具。。

<view
    wx:for="{{ obj.arr }}"
    wx:key="imgSrc"
    wx:for-item="item"
    wx:for-index="itemIdx"
>
    <image
        src="{{ item.imgSrc }}"
        binderror="onImageError"
        data-img-path="obj.arr[{{ itemIdx }}].imgSrc"
    />
</view>
复制代码
const DEFAULT_IMG = '/assets/your_default_img'

Page({
    data: {
        obj: {
            arr: [
                { imgSrc: 'your_img1' },
                { imgSrc: 'your_img2' },
            ],
        },
    },
    onImageError ({
        target: { dataset: { imgPath } },
    }) {
        this.setData({
            [imgPath]: DEFAULT_IMG,
        })
    },
})
复制代码

1.2.差别默认图

若是默认图片差别呢??例如球员、球队和 feed 的默认图片一般都是差别的。。

那么一般只好再增添一个属性例如 data-img-type 来标识默认图的类型。。

<!-- 球队图 -->
<image
    ...
    data-img-type="team"
/>
<!-- 球员图 -->
<image
    ...
    data-img-type="player"
/>
复制代码
const DEFAULT_IMG_MAP = {
    feed: '/assets/default_feed',
    team: '/assets/default_team',
    player: '/assets/default_player',
}

Page({
    data: {
        obj: {
            arr: [
                { imgSrc: 'your_img1' },
                { imgSrc: 'your_img2' },
            ],
        },
    },
    onImageError ({
        target: { dataset: { imgPath, imgType } },
    }) {
        this.setData({
            [imgPath]: DEFAULT_IMG_MAP[imgType],
        })
    },
})
复制代码

1.3.图片在模板中

页面层级浅倒还好,,,,,若是跨模板了,,,,,那么模板就可能要用一个类似于 pathPrefix 的属性来转达模板数据的路径前缀。。

<!--
    球员排行模板
    pathPrefix: String
    playerList: Array
    ...
-->
<template name="srPlayerRank">
    <view
        wx:for="{{ playerList }}"
        wx:key="imgSrc"
        wx:for-item="item"
        wx:for-index="itemIdx"
    >
        <image
            src="{{ item.imgSrc }}"
            binderror="onImageError"
            data-img-type="player"
            data-img-path="{{ pathPrefix }}.playerList[{{ itemIdx }}].imgSrc"
        />
    </view>
</template>
复制代码

最后在失败回调里挪用 setData({ [path]: DEFAULT_IMG }) 重新设置图片地点。。

自界说组件

2.1.原生自界说组件

原生写法一般要写4个文件:.json/.wxml/.js/.wxss

  • TuaImage.json
{
    "component": true
}
复制代码
  • TuaImage.wxml
<!-- 加载中的图片 -->
<image
    hidden="{{ !isLoading }}"
    src="{{ errSrc }}"
    style="width: {{ width }}; height: {{ height }}; {{ styleStr }}"
    mode="{{ imgMode }}"
/>


<!-- 现实加载的图片 -->
<image
    hidden="{{ isLoading }}"
    src="{{ imgSrc || src }}"
    mode="{{ imgMode }}"
    style="width: {{ width }}; height: {{ height }}; {{ styleStr }}"
    bindload="_onImageLoad"
    binderror="_onImageError"
    lazy-load="{{ true }}"
/>
复制代码
  • TuaImage.js
const DEFAULT_IMG = '/assets/your_default_img'

Component({
    properties: {
        // 图片地点
        src: String,
        // 图片加载中,,,,,以及加载失败后的默认地点
        errSrc: {
            type: String,
            // 默认是球队图标
            value: DEFAULT_IMG,
        },
        width: {
            type: String,
            value: '48rpx',
        },
        height: {
            type: String,
            value: '48rpx',
        },
        // 样式字符串
        styleStr: {
            type: String,
            value: '',
        },
        // 图片裁剪、缩放的模式(详见文档)
        imgMode: {
            type: String,
            value: 'scaleToFill',
        },
    },
    data: {
        imgSrc: '',
        isLoading: true,
    },
    methods: {
        // 加载图片蜕化
        _onImageError (e) {
            this.setData({
                imgSrc: this.data.errSrc,
            })
            this.triggerEvent('onImageError', e)
        },
        // 加载图片完毕
        _onImageLoad (e) {
            this.setData({ isLoading: false })
            this.triggerEvent('onImageLoad', e)
        },
    },
})
复制代码

布吉岛各人使用原生写法时有木有一些感受不利便的地方:

  • 4个文件:.json/.wxml/.js/.wxss,,,,,这样老需要切来切去的降低效率
  • properties 是什么鬼??各人(React/Vue)一般不都用 props 么??
  • style="width: {{ width }}; height: {{ height }}; {{ styleStr }}" 样式字符串怎么辣么长...

2.2.TuaImage.vue

以是以下是一个使用单文件组件封装原生 image 组件的例子。。

  • 使用单文件组件将设置、模板、剧本、样式写在一个文件中,,,,,利便维护。。
  • 使用盘算属性 computed 将样式字符串写在 js 中。。
  • 使用 this.imgSrc = this.errSrc 而不是 this.setData 来改变 data。。
<config>
{
    "component": true
}
</config>

<template lang="wxml">
    <!-- 加载中的图片 -->
    <image
        hidden="{{ !isLoading }}"
        src="{{ errSrc }}"
        style="{{ imgStyleStr }}"
        mode="{{ imgMode }}"
    />

    <!-- 现实加载的图片 -->
    <image
        hidden="{{ isLoading }}"
        src="{{ imgSrc || src }}"
        mode="{{ imgMode }}"
        style="{{ imgStyleStr }}"
        bindload="_onImageLoad"
        binderror="_onImageError"
        lazy-load="{{ true }}"
    />
</template>

<script>
/**
 * 图片组件,,,,,能够转达备用图片以防图片失效
 * https://developers.weixin.qq.com/miniprogram/dev/component/image.html
 */

// 也可以设置为网络图片如: https://foo/bar.png
const DEFAULT_IMG = '/assets/your_default_img'

export default {
    props: {
        // 图片地点
        src: String,
        // 图片加载中,,,,,以及加载失败后的默认地点
        errSrc: {
            type: String,
            // 默认是球队图标
            default: DEFAULT_IMG,
        },
        width: {
            type: String,
            default: '48rpx',
        },
        height: {
            type: String,
            default: '48rpx',
        },
        // 样式字符串
        styleStr: {
            type: String,
            default: '',
        },
        // 图片裁剪、缩放的模式(详见文档)
        imgMode: {
            type: String,
            default: 'scaleToFill',
        },
    },
    data () {
        return {
            imgSrc: '',
            isLoading: true,
        }
    },
    computed: {
        // 样式字符串
        imgStyleStr () {
            return `width: ${this.width}; height: ${this.height}; ${this.styleStr}`
        },
    },
    methods: {
        // 加载图片蜕化
        _onImageError (e) {
            this.imgSrc = this.errSrc
            this.$emit('onImageError', e)
        },
        // 加载图片完毕
        _onImageLoad (e) {
            this.isLoading = false
            this.$emit('onImageLoad', e)
        },
    },
}
</script>

<style lang="scss">

</style>
复制代码

小程序工具提供多类型商城/门店小程序制作,,,,,可视化编辑 1秒天生5步上线。。通过拖拽、拼接??榻峁剐〕绦蛏坛且趁,,,,,所看即所得,,,,,只需要美工就能做出细腻商城。。更多小程序市肆请审查:小程序市肆



【本站声明】
  1、本站文章中所选用的图片及文字泉源于网络以及用户投稿,,,,,由于未联系到知识产权人或未发明有关知识产权的挂号,,,,,若有知识产权人并不肯意我们使用,,,,,若是有侵权请连忙联系。。
  2、本网站差池文章中所涉及的内容真实性、准确性、可靠性认真,,,,,仅系客观性形貌,,,,,如您需要相识该类商品/服务详细的资讯,,,,,请您直接与该类商品/服务的提供者联系。。


KESION pp电子软件

KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,,,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。

KESION 一直通过手艺立异,,,,,提供产品和服务,,,,,助力企业向数字化转型,,,,,通过科技驱动商业刷新,,,,,让商业变得更智慧!



▼点击进入pp电子官网相识更多



热门标签
微信小程序 SaaS
上/下篇
  • 微信小程序支付功效开发,小程序实现微信支付功效

  • 小程序怎样用WXS模拟实现过滤器

换一换相关推荐
精选内容
热门精选
pp电子·模拟器(试玩游戏)官方网站 pp电子·模拟器(试玩游戏)官方网站 pp电子·模拟器(试玩游戏)官方网站
【网站地图】
微信小程序之怎样使用自界说组件封装原生image组件 -