Vue.js 在赋值之前无法读取 null/undefined 的属性

Our*_*rBG 2 vuejs2

我有一个来自 的财产axios.get

它在我的dataas null(or ''or [])中定义

呼叫axios处于created挂断状态。

当应用程序首次加载时,控制台会填充

 Error in render: "TypeError: Cannot read property 'service' of null"
Run Code Online (Sandbox Code Playgroud)

当然,它实际上在网页上正确显示了信息,因为serviceis not null,但这个错误显然是在 axios 获取数据之前抛出的。

这里最好的方法是什么?

小智 5

您必须将数据指定为空对象以避免此错误:

    data: {
       your_object: {}
   }
Run Code Online (Sandbox Code Playgroud)

your_object加载数据时具有服务属性的对象在哪里?

(根据我的假设,在 axios.get Promise 函数中,您会执行类似的操作:

your_object.service = result.data;
Run Code Online (Sandbox Code Playgroud)