youzan / vant-weapp

轻量、可靠的小程序 UI 组件库
https://vant.pro/vant-weapp/
MIT License
17.76k stars 3.48k forks source link

van-area 组件初始化报错 #640

Closed secreter closed 6 years ago

secreter commented 6 years ago

注意:请按照下面的模板来新建 issue,不规范的 issue 会被立即关闭.

描述一下问题 mpvue 上按照实例引用可以正常显示,但控制台报错

mpvue代码

<van-area :area-list="areaList" value="110101" />

import areaList from './area'
data () {
    return {
      areaList
    }
  }

报错信息

VM9317:1 thirdScriptError
Cannot read property 'county_list' of null; [Component] Property Observer Error @ static/vant/area/index#(anonymous)
TypeError: Cannot read property 'county_list' of null
    at c.setValues (http://127.0.0.1:61061/appservice/static/vant/area/index.js:126:61)
    at c.observer (http://127.0.0.1:61061/appservice/static/vant/area/index.js:32:14)
    at c.prop.observer (http://127.0.0.1:61061/appservice/static/vant/mixins/observer/props.js:26:18)
    at Function.r.safeCallback (http://127.0.0.1:61061/appservice/__dev__/WAService.js:17:14483)
    at a.<anonymous> (http://127.0.0.1:61061/appservice/__dev__/WAService.js:18:13674)
    at d (http://127.0.0.1:61061/appservice/__dev__/WAService.js:17:23079)
    at i.doUpdates (http://127.0.0.1:61061/appservice/__dev__/WAService.js:17:24291)
    at o.applyProperties (http://127.0.0.1:61061/appservice/__dev__/WAService.js:20:9672)
    at e.value (http://127.0.0.1:61061/appservice/__dev__/WAService.js:20:2387)
    at http://127.0.0.1:61061/appservice/__dev__/WAService.js:20:2459
console.error @ VM9317:1
errorReport @ WAService.js:3
thirdErrorReport @ WAService.js:3
(anonymous) @ WAService.js:3
(anonymous) @ WAService.js:19
r.safeCallback @ WAService.js:17
r.call @ WAService.js:17
a @ WAService.js:17
r.safeCallback @ WAService.js:17
(anonymous) @ WAService.js:18
d @ WAService.js:17
i.doUpdates @ WAService.js:17
o.applyProperties @ WAService.js:20
value @ WAService.js:20
(anonymous) @ WAService.js:20
value @ WAService.js:20
(anonymous) @ WAService.js:20
value @ WAService.js:20
(anonymous) @ WAService.js:20
value @ WAService.js:20
value @ WAService.js:19
p._advancedCreate @ WAService.js:18
p.create @ WAService.js:18
se @ WAService.js:19
a.addView @ WAService.js:19
value @ WAService.js:22
X @ WAService.js:21
re @ WAService.js:21
ie @ WAService.js:21
(anonymous) @ WAService.js:21
(anonymous) @ WAService.js:21
s.emit @ WAService.js:6
emit @ WAService.js:6
(anonymous) @ WAService.js:6
(anonymous) @ WAService.js:5
e @ appservice?t=1537890298907:6793
n.registerCallback.t @ appservice?t=1537890298907:6793
l.forEach.t @ appservice?t=1537890298907:6793
(anonymous) @ appservice?t=1537890298907:6793
i.onmessage @ appservice?t=1537890298907:6793
VM9317:1 thirdScriptError
Cannot read property 'province_list' of null; [Component] Property Observer Error @ static/vant/area/index#(anonymous)
TypeError: Cannot read property 'province_list' of null
    at c.getList (http://127.0.0.1:61061/appservice/static/vant/area/index.js:96:42)
    at c.setValues (http://127.0.0.1:61061/appservice/static/vant/area/index.js:127:27)
    at c.observer (http://127.0.0.1:61061/appservice/static/vant/area/index.js:11:14)
    at c.prop.observer (http://127.0.0.1:61061/appservice/static/vant/mixins/observer/props.js:26:18)
    at Function.r.safeCallback (http://127.0.0.1:61061/appservice/__dev__/WAService.js:17:14483)
    at a.<anonymous> (http://127.0.0.1:61061/appservice/__dev__/WAService.js:18:13674)
    at d (http://127.0.0.1:61061/appservice/__dev__/WAService.js:17:23079)
    at i.doUpdates (http://127.0.0.1:61061/appservice/__dev__/WAService.js:17:24291)
    at o.applyProperties (http://127.0.0.1:61061/appservice/__dev__/WAService.js:20:9672)
    at e.value (http://127.0.0.1:61061/appservice/__dev__/WAService.js:20:2387)

环境

chenjiahan commented 6 years ago

请确认你的 areaList 是否正确传入

secreter commented 6 years ago

官方提供的areaList,https://github.com/youzan/vant/blob/dev/packages/area/demo/area.js

jerryachel commented 6 years ago

遇到同样的问题,mounted中可以正确打印出areaList 了

vopdoo commented 6 years ago

@secreter @jerryachel

可按照如下修改 两处 dist/area/index.js 代码,暂时解决问题

https://github.com/youzan/vant-weapp/blob/c1be15ecb6b09d84b0b7625ef869aea4185eafdd/dist/area/index.js#L116 修改为

var code = this.code || this.data.areaList && Object.keys(this.data.areaList.county_list || {})[0] || '';

https://github.com/youzan/vant-weapp/blob/c1be15ecb6b09d84b0b7625ef869aea4185eafdd/dist/area/index.js#L86

修改为

var list =  this.data.areaList && this.data.areaList[type + "_list"] || {};