Chaunjie / kai-ui

高颜值组件库,简单易用,同时支持原生和wepy框架
https://chaunjie.github.io/kui/dist
214 stars 33 forks source link

Cannot read property 'height' of null #11

Closed xyohn closed 5 years ago

xyohn commented 5 years ago

folder组件 检查了下是onLoad里

 wx.createSelectorQuery().select('.folder-content__' + this.componentId).boundingClientRect((res) => {
        this.height = res.height
        this.$apply()
      }).exec()

里的res.height的问题 试了一下res是null,把select方法里的第一个参数改成.folder-content__0(其中0为传入的id)仍有这个错误 看了很久也没发现代码有什么问题..

Chaunjie commented 5 years ago

你的res出来的是什么

xyohn commented 5 years ago

我试着打印了一下res res是null 一开始有怀疑是.folder-content + this.componentId的拼接问题导致取不到这个节点 但是后来打印.folder-content + this.componentId 是正确的值 ..所以..陷入了迷茫 想问一下 在你那边会有取不到这个值的情况吗.. 我用的是wepy版本..不过我是直接拷贝src/components的folder的 这个应该就没有影响吧...

Chaunjie commented 5 years ago

没有影响,你这样写试试wx.createSelectorQuery().in(this)...

xyohn commented 5 years ago

是将原代码更改为:

      wx.createSelectorQuery().in(this).select('.folder-content__' + this.componentId).boundingClientRect((res) => {
        this.height = res.height
        this.$apply()
      }).exec()

吗 控制台提示An SelectorQuery call is ignored because no proper page or component is found. Please considering using SelectorQuery.in to specify a proper one.

xyohn commented 5 years ago

我先尝试跑一下clone的demo看看会不会有这个问题~

Chaunjie commented 5 years ago

你是不是在未渲染就去获取了?,你尝试下做个延迟能不能取到,如果延迟能取到的话说明你未渲染的时候就去获取dom

xyohn commented 5 years ago

这句话是在folder组件的wepy文件里的.. 原先就有的..我是直接另一个页面引入folder这个组件 访问这个页面的时候报的这个错误.... 不过我刚才编译了下clone的这个 没有这个问题。。。 我把我页面其他代码屏蔽掉。。也没这个问题了。。然后试了一下。。破案了 我的代码是

<block wx:if="{{!abnor.loading}}">
      ....
    </block>
    <block wx:else>
      <folder title="会员信息" componentId="1">
        <view slot="content">
          ...
        </view>
      </folder>
    </block>

如果我的block里是wx:if="{{!abnor.loading}}"的话 就会有这个问题 如果我写的是wx:if="{{!true}}" 都不会有问题... 不明白为什么 =。=

Chaunjie commented 5 years ago

wx:if判断语句导致未渲染

xyohn commented 5 years ago

啊我明白了..因为组件还未渲染但是先执行了组件内的onLoad?那...如果我有这个wx:if的需求的话...有没有什么好的解决方式呢

Chaunjie commented 5 years ago

你可以尝试等!abnor.loading这个条件成立之后再去获取dom

xyohn commented 5 years ago

但是这句话是写在folder组件内的onLoad里的呀..... 感觉直接改组件传入参数告知是否Loading然后好像..不是特别好?

xyohn commented 5 years ago

明白问题缘由啦 这个需求我自己解决 谢谢你啦!

Chaunjie commented 5 years ago

针对你这个问题,给组件传唯一的解决方案,因为组件并不知道啥时候真正渲染了,只能等传过去的值改变了才能知道

xyohn commented 5 years ago

哈哈是的 所以我想了想..只能对原组件开刀了........

Chaunjie commented 5 years ago

对的,因为你这个需求在组件设计的时候是不会考虑这一块,所以只能这么干,还有一种方式就是在原组件外围包一层组件,在中间组件监控数据变化,这种可以避免对原组件的修改,也可以做扩展

xyohn commented 5 years ago

好的明白 谢谢