Tôi có đoạn lệnh trong Vue thế này
template: `
<div>
<div>{{msg}}</div>
<div>size : {{size}}</div>
<button @click="on_add()">Add</button>
<button @click="on_can_add()">Can add</button>
<ol>
<li v-for="x in ds">
{{x}}
<button @click="on_update(x)">Update</button>
<button @click="on_del(x)">Del</button>
</li>
</ol>
</div>
`,
Để cập nhật cái biến size mỗi khi ds thay đổi phải watch thế này
watch: {
"ds":{
handler:function(val,old){
this.get_size();//phải chạy hàm get_size()
}
}
},
Cái hàm get_size() thế này
get_size(){
let ref=firebase.database().ref('TestAccess');
ref.once("value",(ds)=>{
this.size=ds.numChildren();//Khai báo cái size này trong data
})
},
…
Dĩ nhiên đoạn lệnh này vẫn ok nhưng có một số phiền toái là dích cái biến this.size trong ấy
nay tôi muốn trong mục watch chỉ viết thế này
watch: {
"ds":{
handler:function(val,old){
//this.get_size();//phải chạy hàm get_size()//---bỏ
this.size=this.get_size(); //
}
}
},
.....
thì phải làm sao?