赞
踩
iview tree组件:
data () {
return {
baseData: [ ]
}
},
methods: {
LoadData(item,callback){
this.$api.get_staff_cat(item.id,item.catisparent,item.haschildcat).then( function(res) {
callback(res.data);
})
}
},
created () {
this.baseData = [];
this.$api.get_staff_cat().then( res => {
const that = this;
res.data.forEach(function (c) {
that.baseData.push({‘title’:c.title,’loading’:false,’children’:[],’id’:c.cat_id,’isparent’:c.catisparent,’haschildcat’:c.haschildcat
});
})
})
}
运行时报错:
[Vue warn]: Error in callback for watcher “data”: “TypeError: node.children.forEach is not a function”
found in
—>
at src\components\split-pane\split.vue
at src\wkviews\staff\staff_list.vue
at src\views\Main.vue
at src\app.vue
vue.esm.js:578
TypeError: “node.children.forEach is not a function”
flattenChildrenhttp://192.168.1.80:8080/dist/vender-base.js:55512:21flattenChildrenhttp://192.168.1.80:8080/dist/vender-base.js:55513:32flattenChildrenhttp://192.168.1.80:8080/dist/vender-base.js:55512:21compileFlatStatehttp://192.168.1.80:8080/dist/vender-base.js:55518:17compileFlatStatehttp://192.168.1.80:8080/dist/vender-base.js:55517:13boundFnhttp://192.168.1.80:8080/dist/vender-base.js:1011:9handlerhttp://192.168.1.80:8080/dist/vender-base.js:55484:34runhttp://192.168.1.80:8080/dist/vender-base.js:4026:11flushSchedulerQueuehttp://192.168.1.80:8080/dist/vender-base.js:3774:5nextTickhttp://192.168.1.80:8080/dist/vender-base.js:2633:9flushCallbackshttp://192.168.1.80:8080/dist/vender-base.js:2554:5
异步返回的数据:
{“code”:0,”data”:[{“title”:”child”,”loading”:false,”children”:”[]”},{“title”:”child1”,”loading”:false,”children”:”[]”}]}
错误分析:
forEach()是数组才有的方法,报错forEach is not a function就说明node.children不是一个数组。
但它又有这个属性,只是这个属性值是undefined,而不是一个function类型,这就说明node.children是一个对象
看看服务端代码:
这样就清楚了,在$arr里面把children定义成了字符串’[ ]’,json_encode时把它解析成了json对象,而iview tree要求children是
数组,就会出现forEach is not a function错误
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。