uni-app vue2版本Android端instanceof判断响应式数组返回结果不正确

uni-app vue2版本Android端instanceof判断响应式数组返回结果不正确

示例代码:

<template>  
    <view class="content">  
        <image class="logo" src="/static/logo.png"></image>  
        <view class="text-area">  
            <text class="title">{{ title }}</text>  
        </view>  
    </view>  
</template>  

<script>  
export default {  
    data() {  
        return {  
            title: 'Hello',  
            array: ['scooter-zd', 'scooter-town']  
        };  
    },  
    onLoad() {  
        console.log(this.array instanceof Array);  
    },  
    methods: {}  
};  
</script>  

<style>  
.content {  
    display: flex;  
    flex-direction: column;  
    align-items: center;  
    justify-content: center;  
}  

.logo {  
    height: 200rpx;  
    width: 200rpx;  
    margin-top: 200rpx;  
    margin-left: auto;  
    margin-right: auto;  
    margin-bottom: 50rpx;  
}  

.text-area {  
    display: flex;  
    justify-content: center;  
}  

.title {  
    font-size: 36rpx;  
    color: #8f8f94;  
}  
</style>  

操作步骤:

  • 运行到app端

预期结果:

  • 打印true

实际结果:

  • 打印false

bug描述:

  • vue2版本,在app端,instanceof判断响应式数组是否属于Array会返回false,h5端和vue3版本不会

更多关于uni-app vue2版本Android端instanceof判断响应式数组返回结果不正确的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

晚会排查下

更多关于uni-app vue2版本Android端instanceof判断响应式数组返回结果不正确的实战教程也可以访问 https://www.itying.com/category-93-b0.html


应该是原型链的问题,可以先用其他方法比如 Array.isArray

这是一个uni-app在Vue2版本Android端的已知问题,由于响应式系统实现机制导致的。在Vue2中,uni-app对数组进行了响应式包装,导致原始数组原型链被修改,因此instanceof检查会返回false。

解决方案:

  1. 使用Array.isArray()替代instanceof检查:
console.log(Array.isArray(this.array)); // 会返回true
  1. 如果需要保持instanceof检查,可以获取数组的原始值:
console.log(this.$data.array instanceof Array);
回到顶部