h***@163.com
h***@163.com
  • 发布:2019-11-22 20:26
  • 更新:2019-12-17 16:16
  • 阅读:3367

【报Bug】uni-app iOS端 this.$refs为空

分类:uni-app

详细问题描述

.vue文件,引入自定义.vue组件,设置ref,this.$refs 为空

H5 正常,iOS为空

重现步骤

[步骤]

自定义.vue组件

<template>  
    <view class="box" v-show="show">  
        <uni-popup ref="popup" @change="popupChange" :animation="false">  
            <view class="box_card">  
                <text class="box_card_title">{{ info.title }}</text>  
                <text class="box_card_content">{{ info.content }}</text>  
                <text v-show="info.tip_content" class="box_card_tip_content">{{ info.tip_content }}</text>  

                <button class="box_card_btn" @click="hideBox()">好的</button>  
            </view>  
        </uni-popup>  
    </view>  
</template>  

<script>  

    import uniPopup from "@/components/uni-popup/uni-popup.vue"  
    export default {  

        data() {  
            return {  
                show: false,  
                info: {  
                    title: "",  
                    content: "",  
                    tip_content: "",  
                    complete: undefined,  
                },  
            };  
        },  

        components: {  
            uniPopup,  
        },  

        methods: {  

            showBox(info){  
                this.info = info  
                this.show = true // v-if 先渲染父节点,子节点$refs才能拿到  
                this.$refs.popup.open()  

            },  

            hideBox(){  
                this.show = false  
                this.$refs.popup.close()  
            },  

            popupChange(res){  
                if(res.show){  
                    uni.hideTabBar()  
                }else{  
                    uni.showTabBar()  
                    if(this.info.complete){  
                        this.info.complete()  
                    }  
                }  
            },  
        }  
    }  
</script>  

<style lang="scss" scoped>  
    .box{  

        .box_card{  
            width: 600rpx;  
            background: #FFFFFF;  
            border-radius: 30rpx;  

            display: flex;  
            flex-direction: column;  
            align-items: center;  

            .box_card_title{  
                margin-top: 30rpx;  
                font-size: 40rpx;  
                font-weight: 600;  
            }  

            .box_card_content{  
                width: 500rpx;  
                font-size: 30rpx;  
                margin-top: 30rpx;  
            }  

            .box_card_tip_content{  
                margin-top: 30rpx;  
                font-size: 30rpx;  
                color: #DD524D;  
            }  

            .box_card_btn{  
                width: 300rpx;  
                height: 80rpx;  
                color: #5b3301;  
                background: linear-gradient(to right, #FFDF9F, #CCA558);  
                margin-top: 30rpx;  
                margin-bottom: 30rpx;  
                border: none;  
                outline:none;  
                border-radius: 360rpx;  

                display: flex;  
                justify-content: center;  
                align-items: center;  
            }  
            .box_card_btn:after{  
                border: none;  
            }  
        }  
    }  
</style>  

引入自定义组件,使用

<template>  
    <view class="phone_login_box" v-show="show">  
        <help-box ref="helpBox"></help-box>  
        <view @click="showBox">显示HelpBox</view>  
    </view>  
</template>  

<script>  
    import HelpBox from '../components/help-box.vue'  
    export default {  
            data() {  
                return {  

                }  
            },  

            components:{  
                HelpBox  
            },  

            methods: {  
                showBox(){  
                    // 此处 this.$refs 为空  
                    this.$refs.helpBox.showBox({  

                    })  
                },  

            }  
        }  

</script>  

<style lang="scss" scoped>  

</style>

[结果]

this.$refs 为空(iOS端为空,H5正常)

[期望]

this.$refs 有 helpBox

IDE运行环境说明

HBuilderX 2.4.2.20191115

macOS Mojave 10.14.6 (18G1012)

uni-app运行环境说明

HBuilderX创建的项目,全部默认的配置

App运行环境说明

异常环境iOS
iPhone7 Plus
iOS 12.4

正常环境H5
macOS Mojave 10.14.6 - Chrome 78.0.3904.97

附件

<img src="https://i.loli.net/2019/11/22/1gO8BsqAviWcUVP.jpg"/>

联系方式

[QQ] 1816738462

2019-11-22 20:26 负责人:无 分享
已邀请:
h***@163.com

h***@163.com (作者)

【楼主结贴】经过排查,发现是因为uniapp的一个Bug,因为我在main.js 全局引入组件,然后使用,在浏览器端是正常OK的,但是在App好像出了Bug,不能全局引入,导致页面上根本没有这个组件,那就更没有ref了。所以解决方法是,舍弃全局引入,在使用的页面单独一个个引入

import HelpBox from './../components/help_box/help_box.vue'
Vue.component('help-box',HelpBox)

改成在每个页面单独引入使用吧

<template>
<view>
<help-box ref='helpBox'></help-box>
</view>
</template>

<script>
import HelpBox from '../../components/help_box/help_box.vue'
export default {
components:{
HelpBox,
},
}
</script>

<style lang="scss">
</style>

wenju

wenju - https://www.mescroll.com -- 精致的下拉刷新和上拉加载组件

全局注册的组件路径 可以试试 import HelpBox from '@/components/help_box/help_box.vue'

该问题目前已经被锁定, 无法添加新回复