【发布时间】:2020-05-09 06:29:44
【问题描述】:
我一直在尝试使用 axios 通过 Vue 加载数据,但是得到了未定义的错误。我很确定我的代码没有任何问题,因为它适用于另一个人。我认为问题是在 axios 有机会获取数据之前呈现页面。
async editGoods(oldGoods) {
oldGoods.property1 = await this.$http.loadData("/item/someporperty/" + oldGoods.id);
oldGoods.property2 = await this.$http.loadData("/item/otherproperties/list?id=" + oldGoods.id);
this.isEdit = true;
this.show = true;
this.oldGoods = oldGoods;
}
下面是我的axios配置
import Vue from 'vue'
import axios from 'axios'
import config from './config'
axios.defaults.baseURL = config.api;
axios.defaults.timeout = 2000;
axios.loadData = async function (url) {
const resp = await axios.get(url);
return resp.data;
}
Vue.prototype.$http = axios;
这是我不断收到的错误消息:
vue.esm.js?efeb:1741 TypeError: Cannot read property 'length' of undefined
at eval (eval at ./node_modules/vue-loader/lib/template-compiler/index.js?
{"id":"data-v-c02e22a2","hasScoped":true,"transformToRequire":{"video":
["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":
{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?
type=template&index=0!./src/pages/item/GoodsForm.vue (4.js:276),
<anonymous>:332:47)
at Proxy.renderList (vue.esm.js?efeb:3705)
at Proxy.render (eval at ./node_modules/vue-loader/lib/template-
compiler/index.js?{"id":"data-v-
c02e22a2","hasScoped":true,"transformToRequire":{"video":
["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":
{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?
type=template&index=0!./src/pages/item/GoodsForm.vue (4.js:276),
<anonymous>:320:23)
at VueComponent.Vue._render (vue.esm.js?efeb:4544)
at VueComponent.updateComponent (vue.esm.js?efeb:2788)
at Watcher.get (vue.esm.js?efeb:3142)
at Watcher.run (vue.esm.js?efeb:3219)
at flushSchedulerQueue (vue.esm.js?efeb:2981)
at Array.eval (vue.esm.js?efeb:1837)
at MessagePort.flushCallbacks (vue.esm.js?efeb:1758)
大家可以看一下完整的代码here和here。 GoodsForm.vue 是被渲染的表单。
已经尝试了几天,有人知道如何处理吗?
【问题讨论】:
-
你在哪里使用了“长度”?在 v-for 中?
-
你能显示你使用“show”的地方的代码吗