[英]Vue warning Error in data() "TypeError: Cannot convert undefined or null to object"
does anybody know how I can get rid of this warning?有谁知道我怎样才能摆脱这个警告? I guess it happens because I try to do something with an undefined object?我猜这是因为我试图用未定义的对象做某事? Any help would be very much appreciated!任何帮助将不胜感激! Thank you!谢谢! Here is my code:这是我的代码:
<v-card class="ma-3 pa-3" v-for="item in state.monitorAll[0]" v-bind:key="item.countdown"> <v-row> <div class="ma-3">Minuten bis zum nächsten Halt: <v-avatar class="ml-2 blue white--text">{{ item.departureTime.countdown }}</v-avatar></div> <v-avatar v-if="typeof item.vehicle == 'undefined'" class="ma-3 ml-5 green"><v-icon class="white--text">accessible</v-icon></v-avatar> </v-row> </v-card> <script> import axios from 'axios' import {onMounted, reactive} from '@vue/composition-api' export default { name: 'App', setup() { let state = reactive({ wlData: '', monitorAll: [], }) async function showData() { let config = { headers: { 'Accept': 'application/json', 'Access-Control-Allow-Origin': 'http://localhost:8080', 'Content-Type': 'text/plain' } } const wlData = await axios.get('http://localhost:8080/ogd_realtime/monitor?rbl=832', config); state.wlData = wlData.data.data['monitors']; state.monitorAll = state.wlData.map(monitor => monitor.lines[0].departures.departure); } let startLoad = onMounted(() => { console.log('component is mounted!'); showData(); }) return { state, showData, startLoad, } } }; </script>
I figured it out.我想到了。 It was the onMounted that caused the problem.是 onMounted 导致了问题。 I guess it is because the onMounted is called before there is any value in the showData().我猜这是因为在 showData() 中有任何值之前调用了 onMounted。 I am not sure.我不确定。 I deleted;我删除了;
let startLoad = onMounted(() => { console.log('component is mounted!'); showData(); }) let startLoad = onMounted(() => { console.log('组件已安装!'); showData(); })
and just called showData() directly after the async function showData() is being created.并在创建异步函数 showData() 后直接调用 showData() 。 I have no idea why this works and I guess it is not the best way to handle it, but at least I lost my warning!我不知道为什么会这样,我想这不是处理它的最佳方法,但至少我失去了警告!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.