我需要在模板编译前进行2次异步操作。
methods: {
getResults(){
return new Promise(resolve => {
if (this.$store.state.results) {
this.results = this.$store.state.results
resolve()
}
else {
const q = this.$route.query.q
this.$http.get('search/', {params: {q: q}}).then(response => {
this.$store.commit('postResults', {results: response.data.results, count: response.data.count})
resolve()
})
}
})
},
getDetail(){
return this.$http('vendor/detail/',{params:{
doc:id
}})
}
},
beforeMount(){
this.getResults().then(this.getDetail).then((response)=>{
this.detail=response
})
},
现在的情况是,getDetail执行完后模板就开始编译了。。求大神解答:(
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这样做的结果是
vue.common.js?e881:433 TypeError: Cannot read property 'id' of undefined(…)但是我发现,如果我把模板的这里
改为
这样做页面可以顺利渲染
有点不解,希望能解答.. 谢谢
问题应该在这:
v-for依赖的是第一次异步的结果results,但是模板却需要渲染第二次异步才能获取到detail..我是这么搞的:
有点不优雅,但是解决了。有优雅的解决方案吗