我试图根据我的Firebase数据库中的值更新值。即使我可以通过控制台显示更新的值,功能也无法更改开始时指定的值。我该如何解决这个问题?
let app = firebase.initializeApp(firebaseConfig);
let postss = app.database().ref('posts');
let politics = 0;
let software = 0;
let history = 0;
let travel = 0;
function gotData(data){
let categori = data.val();
let keys = Object.keys(categori);
for(let i = 0; i < keys.length; i++){
let k = keys[i];
let categoriezz = categori[k].categories;
for(let dates = 0; dates < categoriezz.length; dates++){
if(categoriezz[dates] == "Politics"){
politics++;
}
else if(categoriezz[dates] == "Travel"){
travel++;
}
else if(categoriezz[dates] == "Software"){
software++;
}
else if(categoriezz[dates] == "History"){
history++;
}
}
//console.log(categoriezz,politics,travel,software,history);
}
console.log(politics,travel,software,history);
}
function errData(err){
console.log(err);
}
export default {
extends: Pie,
props: ['chartdata', 'options'],
mounted () {
postss.on('value', gotData, errData);
this.renderChart({
labels: ['Politics','Software','History','Travel'],
datasets: [
{
label: 'Topics',
backgroundColor: ['red','green','brown','blue'],
data: [politics, software, history, travel]
}
]
}, {responsive: true, maintainAspectRatio: false})
}
}
</script>
edit:我发现,只要用导航栏更改页面,它就会根据这些值重复并更新图表。唯一的问题是,它在您第一次加载页面时不会更新值。