我正在从另一个视图导航到一个视图,并将 itemdId 作为参数值传递给 vue 路由器。
我希望能够使用该 itemId 调用 firebase,以便我可以过滤数据并在 UI 中使用过滤后的结果/数据。我正在使用vuefire
.
发生的事情是 vue 在 created() 中的数据可用之前开始渲染,我看到错误是控制台说视图正在引用未定义的属性值。
有没有办法在数据可用后渲染视图?
我已经尝试过 usingbeforeMount
以及 ascreated
和beforeCreate
方法。
请参见下面的代码:
<h1>{{projects[0].project_name}}</h1> //it says type error. cannot read property project_name of undefined. Sometimes on refresh it works but most times it does not.
脚本代码如下:
let projectsRef = db.ref('projects');
export default {
name: 'ProjectDetailsOpenInvesting',
props: {
data: Object
},
firebase:{
projects: projectsRef
},
data(){
return {
projects:[],
.....
}
},
created(){
var that = this;
console.log(this.$route.params.itemid) //this works
this.projects = this.projects.filter(function(p){
return p.project_id == that.$route.params.itemid //this works as well
})
}