动态更新 ref Vuejs 和 Vuefire
Updating ref dynamically Vuejs and Vuefire
首先,我是一名设计专业的学生,我使用vuejs 为我的高级项目制作原型。所以这不是真正的工作项目。只是一些日记应用程序的原型。
基本上我的问题是,我有一个呈现我的 firebase 数组的列表组件。在 firebase 中,每日数组使用由我手动编辑的键调用。每个键代表一天,例如 20180519、20180520。并且,有一个日期选择器组件将这种格式 (YYYYMMDD) 的日期发送到 firebase.js,这是我的 firebase 配置文件。
列表组件结构如下:
<template>
<div id="list">
<ul>
<li v-show="current" v-for="(item, index) in list"></li>
<li v-show="!current" v-for="(item, index) in prevList"></li>
</ul>
</div>
</template>
<script>
import { today, prevList } from '.././firebase';
export default {
name: 'List',
data() {
return {
current: true,
list: [],
prevList: [],
}
},
mounted() {
this.$root.$on('checkYesterday', data => {
this.current = data
});
},
firebase: {
list: today,
prevList: prevList
},
};
</script>
我的日期选择器组件通过 EventBus 将日期发送到 firebase.js。就像
<template>
<datepicker v-on:selected="doSelection"><datepicker>
</template>
<script>
import moment from 'moment';
import Datepicker from 'vuejs-datepicker';
import { EventBus } from '.././firebase';
export default {
name: 'Header',
data() {
return {
date: moment().format('LL'),
openDatePicker: false,
};
},
methods: {
doSelection(val) {
const pickerValue = moment(val).format('YYYYMMDD');
const currentDay = moment().format('YYYYMMDD');
EventBus.$emit('selectedDayEvent', pickerValue);
if (pickerValue === currentDay) {
this.$root.$emit('checkYesterday', true)
} else {
this.$root.$emit('checkYesterday', false)
}
}
},
components: {
Datepicker,
}
};
</script>
因此,当用户选择一天时,EventBus 将日期发送到 firebase.js 并且还会发送为其呈现列表的列表组件。
firebase.js file is like,
import { initializeApp } from 'firebase';
import moment from 'moment';
import Vue from 'vue';
export const EventBus = new Vue({
data: {
daySelected: '',
}
});
const app = initializeApp({
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "",
messagingSenderId: "..."
})
const day = moment().format('YYYYMMDD');
EventBus.$on('selectedDayEvent', pickerValue => {
this.daySelected = pickerValue;
});
export const db = app.database();
export const today = db.ref(`${day}`);
export const prevList = db.ref(`${this.daySelected}`);
今天在 firebase.js 中正确执行,但是当通过日期选择器选择日期时,prevList 不会更新。但是当日期通过 EventBus 中的日期选择器更改时,daySelected 变量会更新。
如何更新 prevList
?
根据document
This function will get executed only once. If you want to have
automatic rebind (pretty much like a computed property) use a $watch
and call $unbind and then $bindAsArray
您可能希望将 daySelected
存储为数据 属性:
<script>
import { today, EventBus, db } from '.././firebase';
export default {
name: 'List',
data() {
return {
current: true,
list: [],
prevList: [],
daySelected: ''
}
},
created() {
this.$bindAsArray('prevList', db.ref(`${this.daySelected}`))
},
mounted() {
this.$root.$on('checkYesterday', data => {
this.current = data
});
EventBus.$on('selectedDayEvent', pickerValue => {
this.daySelected = pickerValue;
});
},
watch: {
daySelected: function() {
if (this.$firebaseRefs['prevList']) {
this.$unbind('prevList')
}
this.$bindAsArray('prevList', db.ref(`${this.daySelected}`))
}
},
firebase () {
return {
list: today
}
}
};
</script>
首先,我是一名设计专业的学生,我使用vuejs 为我的高级项目制作原型。所以这不是真正的工作项目。只是一些日记应用程序的原型。
基本上我的问题是,我有一个呈现我的 firebase 数组的列表组件。在 firebase 中,每日数组使用由我手动编辑的键调用。每个键代表一天,例如 20180519、20180520。并且,有一个日期选择器组件将这种格式 (YYYYMMDD) 的日期发送到 firebase.js,这是我的 firebase 配置文件。
列表组件结构如下:
<template>
<div id="list">
<ul>
<li v-show="current" v-for="(item, index) in list"></li>
<li v-show="!current" v-for="(item, index) in prevList"></li>
</ul>
</div>
</template>
<script>
import { today, prevList } from '.././firebase';
export default {
name: 'List',
data() {
return {
current: true,
list: [],
prevList: [],
}
},
mounted() {
this.$root.$on('checkYesterday', data => {
this.current = data
});
},
firebase: {
list: today,
prevList: prevList
},
};
</script>
我的日期选择器组件通过 EventBus 将日期发送到 firebase.js。就像
<template>
<datepicker v-on:selected="doSelection"><datepicker>
</template>
<script>
import moment from 'moment';
import Datepicker from 'vuejs-datepicker';
import { EventBus } from '.././firebase';
export default {
name: 'Header',
data() {
return {
date: moment().format('LL'),
openDatePicker: false,
};
},
methods: {
doSelection(val) {
const pickerValue = moment(val).format('YYYYMMDD');
const currentDay = moment().format('YYYYMMDD');
EventBus.$emit('selectedDayEvent', pickerValue);
if (pickerValue === currentDay) {
this.$root.$emit('checkYesterday', true)
} else {
this.$root.$emit('checkYesterday', false)
}
}
},
components: {
Datepicker,
}
};
</script>
因此,当用户选择一天时,EventBus 将日期发送到 firebase.js 并且还会发送为其呈现列表的列表组件。
firebase.js file is like,
import { initializeApp } from 'firebase';
import moment from 'moment';
import Vue from 'vue';
export const EventBus = new Vue({
data: {
daySelected: '',
}
});
const app = initializeApp({
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "",
messagingSenderId: "..."
})
const day = moment().format('YYYYMMDD');
EventBus.$on('selectedDayEvent', pickerValue => {
this.daySelected = pickerValue;
});
export const db = app.database();
export const today = db.ref(`${day}`);
export const prevList = db.ref(`${this.daySelected}`);
今天在 firebase.js 中正确执行,但是当通过日期选择器选择日期时,prevList 不会更新。但是当日期通过 EventBus 中的日期选择器更改时,daySelected 变量会更新。
如何更新 prevList
?
根据document
This function will get executed only once. If you want to have automatic rebind (pretty much like a computed property) use a $watch and call $unbind and then $bindAsArray
您可能希望将 daySelected
存储为数据 属性:
<script>
import { today, EventBus, db } from '.././firebase';
export default {
name: 'List',
data() {
return {
current: true,
list: [],
prevList: [],
daySelected: ''
}
},
created() {
this.$bindAsArray('prevList', db.ref(`${this.daySelected}`))
},
mounted() {
this.$root.$on('checkYesterday', data => {
this.current = data
});
EventBus.$on('selectedDayEvent', pickerValue => {
this.daySelected = pickerValue;
});
},
watch: {
daySelected: function() {
if (this.$firebaseRefs['prevList']) {
this.$unbind('prevList')
}
this.$bindAsArray('prevList', db.ref(`${this.daySelected}`))
}
},
firebase () {
return {
list: today
}
}
};
</script>