乐趣区

关于SegmentFault:uniapp-微信小程序-key失效

uniapp 代码

<template>
    <view>
        <image v-for="(item, i) in fileList" :key="item[urlKey]" :src="item[urlKey]"></image>
    </view>
</template>

<script>
    export default {
        props: {urlKey: {default: 'url'},
            fileList: Array
        }
    }
</script>

编译到 微信小程序

<view>
    <block wx:for="{{fileList}}" wx:for-item="item" wx:for-index="i" wx:key="urlKey">
        <image src="{{item[urlKey]}}"></image>
    </block>
</view>

貌似不反对 :key=”item[urlKey]” 这种语法



解决方案:

<template>
    <view>
        <image v-for="(item, i) in fileList" :key="key(item)" :src="item[urlKey]"></image>
    </view>
</template>

<script>
    export default {
        props: {urlKey: {default: 'url'},
            fileList: Array
        },
        computed: {key() {return e => e[this.urlKey]
            }
        }
    }
</script>

应用 computed 就能够解决了



退出移动版