5***@qq.com
5***@qq.com
  • 发布:2022-04-04 15:52
  • 更新:2022-04-04 22:52
  • 阅读:487

nvue页面 uni-app编译模式 onReady()中使用exec()报Cannot read property 'ref' of undefined

分类:nvue
<template>  
    <div>  
        <scroller>  
            <view class="header-img-view">  
                <image v-if="sortList.length!=0" id="header-image" class="header-image" :src="sortList[0]['picUrl']" mode="aspectFit"></image>  
            </view>  
            <scroll-view ref="tabbar1" id="tab-bar" class="tab-bar" :scroll="false" :scroll-x="true" :show-scrollbar="false" :scroll-into-view="scrollInto">  
              <view style="flex-direction: column;">  
                <view style="flex-direction: row;">  
                  <view  class="uni-tab-item" v-for="(tab,index) in sortList" :key="tab.id" :id="tab.id" :ref="'tabitem'+index" :data-id="tab.id" :data-current="index" @click="ontabtap">  
                    <text class="uni-tab-item-title" :class="tabIndex==index ? 'uni-tab-item-title-active' : ''">{{tab.sort}}</text>  
                  </view>  
                </view>  
                <view class="scroll-view-indicator">  
                  <view ref="underline" class="scroll-view-underline" :class="isTap ? 'scroll-view-animation':''" :style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}"></view>  
                </view>  
              </view>  
            </scroll-view>  
            <swiper v-if="sortList.length!=0" class="tab-view" ref="swiper1" id="tab-bar-view" :current="tabIndex" :duration="300" @change="onswiperchange"  
                @transition="onswiperscroll" @animationfinish="animationfinish" @onAnimationEnd="animationfinish">  
                <swiper-item class="swiper-item" v-for="(page, index) in sortList" :key="index">  
                    <!-- <swiper-page class="swiper-page" :pid="page.pageid" :parentId="pageId" ref="page"></swiper-page> -->  
                </swiper-item>  
            </swiper>  
        </scroller>  
        <uni-fab class="c-fab" :pattern="patternFab" horizontal="left" direction="horizontal" :content="contentFab" @trigger="fabButtonClick" @fabClick="fabClick"></uni-fab>  
    </div>  
</template>  

<script>  
    export default {  
        name: 'index',  
        data() {  
            return {  
                sortList: [],  
                fabLastIndex: 0,  
                scrollInto: "",  
                indicatorLineLeft: 0,  
                tabIndex: 0,  
                indicatorLineWidth: 0,  
                isTap: false,  
                pageHeight: 0,  
                _lastTabIndex: 0,  
                swiperWidth: 0,  
                tabbarWidth: 0,  
                tabListSize: 0,  
                _touchTabIndex: 0,  
                _headHeight: 100,  
                pageList: [],  
                contentFab: [  
                    {  
                        text: '新增消费',  
                        iconPath: '/static/img/expend_u.png',  
                        selectedIconPath: '/static/img/expend_o.png',  
                        active: false  
                    },  
                    {  
                        text: '消费统计',  
                        iconPath: '/static/img/count_u.png',  
                        selectedIconPath: '/static/img/count_o.png',  
                        active: false,  

                    }  
                ],  
                patternFab: {  
                    color: 'black',  
                    selectedColor: '#bc3e5e',  
                    backgroundColor: '#eb7b93',  
                    buttonColor: '#bc3e5e',  
                }  
            }  
        },  
        onReady() {  
            this.pageHeight = uni.getSystemInfoSync().windowHeight;  
            this._lastTabIndex = 0;  
            this.swiperWidth = 0;  
            this.tabbarWidth = 0;  
            this.tabListSize = {};  
            this._touchTabIndex = 0;  
            this._headHeight = 100;  
            this.pageList = this.$refs.page;  
            this.selectorQuery()  
        },  
        mounted: async function () {  
          this.sortList = await this.$store.dispatch("queryAllSort").then(data => {  
                    return data  
            })  
            console.log('分类', this.sortList)  
        },  
        methods: {  
            imgButtonClick(title) {  
                uni.showToast({  
                    title: title  
                })  
            },  
            viewLongPress(data) {  
                uni.showToast({  
                    title: "长按触发"+data,  
                })  
            },  
            fabButtonClick(e) {  
                this.contentFab[this.fabLastIndex].active = false  
                this.contentFab[e.index].active = !e.item.active  
                this.fabLastIndex = e.index  
            },  
            fabClick() {  
                this.contentFab[this.fabLastIndex].active = false  
            },  
            ontabtap(e) {  
              let index = e.target.dataset.current || e.currentTarget.dataset.current;  
                this.isTap = false  
                this.tabIndex = index;  
            },  
            switchTab(index) {  

            },  
            updateIndicator(left, width) {  
              this.indicatorLineLeft = left;  
              this.indicatorLineWidth = width;  
            },  
            selectorQuery() {  
              // #ifdef APP-NVUE  
                uni.createSelectorQuery().in(this).select("#header-image").boundingClientRect().exec(rect => {  
                    this._headHeight = rect[0].height  
                })  
              // 查询 tabbar 宽度  
              uni.createSelectorQuery().in(this).select('#tab-bar').boundingClientRect().exec(rect => {  
                this.tabbarWidth = rect[0].width;  
              });   
              // 查询 tabview 宽度  
              uni.createSelectorQuery().in(this).select('#tab-bar-view').boundingClientRect().exec(rect => {  
                this.swiperWidth = rect[0].width;  
            });           
              var queryTabSize = uni.createSelectorQuery().in(this);  
              for (var i = 0; i < this.sortList.length; i++) {  
                queryTabSize.select('#' + this.sortList[i].id).boundingClientRect();  
              }  
              queryTabSize.exec(rects => {  

                rects.forEach((rect) => {  
              this.tabListSize[rect.dataset.id] = rect;  
        })  
        this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex].width);  
                    this.switchTab(this.tabIndex);  
            });  
              // #endif  
            },  
        }  
    }  
</script>  

<style>  
    .header-img-view {  
        background-color: #f8bcca;  
        height: 250rpx;  
        width: 750rpx;  
        align-items: center;  
    }  
    .header-image {  
        height: 250rpx;  
        width: 250rpx;  
    }  
    .list-item {  

    }  
    .tab-bar {  
    width: 750rpx;  
    height: 84rpx;  
    flex-direction: row;  
     /* #ifndef APP-PLUS  
    white-space: nowrap;  
    /* #endif */  
  }  
    .uni-tab-item-title {  
      color: #ffffff;  
      font-size: 30rpx;  
      height: 80rpx;  
      line-height: 80rpx;  
      flex-wrap: nowrap;  
      /* #ifndef APP-PLUS */  
      white-space: nowrap;  
      /* #endif */  
    }  
    .scroll-view-indicator {  
      position: relative;  
      height: 2px;  
      background-color: transparent;  
    }  
    .scroll-view-underline {  
      position: absolute;  
      top: 0;  
      bottom: 0;  
      width: 0;  
      background-color: #ff007f;  
    }  
    .scroll-view-animation {  
      transition-duration: 0.2s;  
      transition-property: left;  
    }  
    .uni-tab-item {  
      /* #ifndef APP-PLUS */  
      display: inline-block;  
      /* #endif */  
      flex-wrap: nowrap;  
      padding-left: 25px;  
      padding-right: 25px;  
    }  
    .uni-tab-item-title-active {  
      color: #ff007f;  
    }  
</style>  
2022-04-04 15:52 负责人:无 分享
已邀请:
5***@qq.com

5***@qq.com (作者)

onReady()延迟一秒调用exec()代码就顺利运行,应该是在执行onReady时页面还没渲染好,拿不到节点信息,钩子函数这么呆吗。同时onReady()也拿不到refs,用this.$nextTick()也拿不到,延迟一秒才行。页面也没渲染什么东西啊.

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