Charles Z.

Results 2 comments of Charles Z.

Here's a solution: reference:[Accessing-Child-Component-Instances-amp-Child-Elements](https://vuejs.org/v2/guide/components-edge-cases.html#Accessing-Child-Component-Instances-amp-Child-Elements) Accessing _$refs_ from within templates or computed propertie should be avoided as _$refs_ are only populated after the component has been rendered, and they are not...

产生报错的操作为点击缩略图,追踪堆栈后发现报错部分 `Cannot read property 'x' of undefined` 一级堆栈发生在PhotoSwipe的源码[pageswipe.js:497](https://github.com/dimsemenov/PhotoSwipe/blob/master/dist/photoswipe.js#L497) `x = _mainScrollPos.x + delta * _options.mainScrollEndFriction;` 其中`_mainScrollPos`来自`_getEmptyPoint()` 二级堆栈发生在[pageswipe.js:2661](https://github.com/dimsemenov/PhotoSwipe/blob/master/dist/photoswipe.js#L2661) 分析调用关系`initialPanOffset`->`_panOffset` ->`_getEmptyPoint()` 报错是由_mainScrollPos为undefined所致,而`_getEmptyPoint`函数只是返回一个简单对象`{x:0,y:0}`,在无法推知执行先后顺序的情况下 只能推测_getEmptyPoint执行前被阻塞了 最后解决还是参考了PS的[Issue#666](https://github.com/dimsemenov/PhotoSwipe/issues/666)、[Issue#1410](https://github.com/dimsemenov/PhotoSwipe/issues/1410)和[Issue#1361](https://github.com/dimsemenov/PhotoSwipe/issues/1361)(17年6月),作者提到了当传入图片参数没有w和h的情况下,就会报这个错,但是作者提到会在之后的版本加入默认的图片大小来处理这个问题,所以讲道理@LS1231 把(快1年前的)源码内的PS依赖版本更新一下应该就能解决。 我的情况:传入的数组图片参数里没有h,但是有w,会报这个错(试了一下只有h没有w则不会报错) 解决办法:computed里遍历数组的时候手动封装一下默认值的逻辑使得w、h必存在