如何正确等待 vue.js 中获取的数据以便在另一种方法中正确使用
How to properly wait for fetched data in vue.js for proper usage in another method
我如何正确地等待获取成功,以便执行 loadMarkers() 方法不会因为 monitorsData.monitors 未定义 而引发错误?
我试过在 fetch 前面使用 await 但我也不太明白如何正确使用它。
data(){
return{
monitorsData:'',
timer:'',
loading:false,
message:'60200950',
markers: [],
}
},
created(){
this.searchStation()
this.loadMarkers()
this.timer = setInterval(this.searchStation, 50000)
},
methods:{
loadMarkers(){
for(let i = 0; i < this.monitorsData.monitors.length; i++){
this.markers.push({id: i, position: {lat: this.monitorsData.monitors[i].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[i].locationStop.geometry.coordinates[1]}})
}
},
searchStation(){
this.loading=true
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'http://www.wienerlinien.at/ogd_realtime/monitor?DIVA='+ this.message+ '&activateTrafficInfo=stoerungkurz'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(jsonData => this.monitorsData = jsonData.data)
.catch(e => console.log(e))
this.loading = false
this.message = ''
},
},
此外,我不知道如何防止计算中出现同样的问题
computed:{
mapConfig() {
return{
...mapSettings,
zoom: 8,
center: {lat: this.monitorsData.monitors[0].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[0].locationStop.geometry.coordinates[1]}
}
}
},
fetch finalize后可以调用该方法:
searchStation(){
this.loading=true
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'http://www.wienerlinien.at/ogd_realtime/monitor?DIVA='+ this.message+ '&activateTrafficInfo=stoerungkurz'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(jsonData => {
this.loading = false
this.message = ''
this.monitorsData = jsonData.data;
this.loadMarkers(); // Call method here
})
.catch(e => console.log(e))
},
将你的 loading
和 message
移到你的 then 链中
searchStation(){
this.loading=true
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'http://www.wienerlinien.at/ogd_realtime/monitor?DIVA='+ this.message+ '&activateTrafficInfo=stoerungkurz'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(jsonData => {
this.monitorsData = jsonData.data
this.loading = false
this.message = ''
})
.catch(e => console.log(e))
},
好的,现在您可以使用 if 语句来检查它是否仍在加载:
loadMarkers(){
if(this.loading) return; //if its true, the function will stop here
for(let i = 0; i < this.monitorsData.monitors.length; i++){
this.markers.push({id: i, position: {lat: this.monitorsData.monitors[i].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[i].locationStop.geometry.coordinates[1]}})
}
},
也应该适用于您的计算 属性:
mapConfig() {
if(this.loading) return;
return{
...mapSettings,
zoom: 8,
center: {lat: this.monitorsData.monitors[0].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[0].locationStop.geometry.coordinates[1]}
}
}
我如何正确地等待获取成功,以便执行 loadMarkers() 方法不会因为 monitorsData.monitors 未定义 而引发错误?
我试过在 fetch 前面使用 await 但我也不太明白如何正确使用它。
data(){
return{
monitorsData:'',
timer:'',
loading:false,
message:'60200950',
markers: [],
}
},
created(){
this.searchStation()
this.loadMarkers()
this.timer = setInterval(this.searchStation, 50000)
},
methods:{
loadMarkers(){
for(let i = 0; i < this.monitorsData.monitors.length; i++){
this.markers.push({id: i, position: {lat: this.monitorsData.monitors[i].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[i].locationStop.geometry.coordinates[1]}})
}
},
searchStation(){
this.loading=true
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'http://www.wienerlinien.at/ogd_realtime/monitor?DIVA='+ this.message+ '&activateTrafficInfo=stoerungkurz'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(jsonData => this.monitorsData = jsonData.data)
.catch(e => console.log(e))
this.loading = false
this.message = ''
},
},
此外,我不知道如何防止计算中出现同样的问题
computed:{
mapConfig() {
return{
...mapSettings,
zoom: 8,
center: {lat: this.monitorsData.monitors[0].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[0].locationStop.geometry.coordinates[1]}
}
}
},
fetch finalize后可以调用该方法:
searchStation(){
this.loading=true
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'http://www.wienerlinien.at/ogd_realtime/monitor?DIVA='+ this.message+ '&activateTrafficInfo=stoerungkurz'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(jsonData => {
this.loading = false
this.message = ''
this.monitorsData = jsonData.data;
this.loadMarkers(); // Call method here
})
.catch(e => console.log(e))
},
将你的 loading
和 message
移到你的 then 链中
searchStation(){
this.loading=true
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = 'http://www.wienerlinien.at/ogd_realtime/monitor?DIVA='+ this.message+ '&activateTrafficInfo=stoerungkurz'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(jsonData => {
this.monitorsData = jsonData.data
this.loading = false
this.message = ''
})
.catch(e => console.log(e))
},
好的,现在您可以使用 if 语句来检查它是否仍在加载:
loadMarkers(){
if(this.loading) return; //if its true, the function will stop here
for(let i = 0; i < this.monitorsData.monitors.length; i++){
this.markers.push({id: i, position: {lat: this.monitorsData.monitors[i].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[i].locationStop.geometry.coordinates[1]}})
}
},
也应该适用于您的计算 属性:
mapConfig() {
if(this.loading) return;
return{
...mapSettings,
zoom: 8,
center: {lat: this.monitorsData.monitors[0].locationStop.geometry.coordinates[0], lng: this.monitorsData.monitors[0].locationStop.geometry.coordinates[1]}
}
}