我碰到過這種情況,因爲我使用的組件包裝器具有UID
作爲屬性,如果UID
爲null顯示等待消息/動畫else顯示您的原始組件。
我真正的情況是有點複雜,在這裏(火力,路由,vuex)發佈,但基本上是包裝組件應該類似於此
<template>
<component :is="CurrentComponent" />
</template>
<script>
import App from './App';
import WaitingAnimation from './WaitingAnimation';
export default {
data() {
return {
Uid: null,
}
},
computed: {
CurrentComponent() {
return this.Uid == null ? WaitingAnimation : App;
}
}
beforeMount() {
//While Firebase is initializing `Firebase.auth().currentUser` will be null
let currentUser = Firebase.auth().currentUser;
//Check currentUser in case you previously initialize Firebase somewhere else
if (currentUser) {
//if currentUser is ready we just finish here
this.Uid = currentUser.uid;
} else {
// if currentUser isn't ready we need to listen for changes
// onAuthStateChanged takes a functions as callback and also return a function
// to stop listening for changes
let authListenerUnsuscribe = Firebase.auth().onAuthStateChanged(user => {
//onAuthStateChanged can pass null when logout
if (user) {
this.Uid = user.uid;
authListenerUnsuscribe(); //Stop listening for changes
}
});
}
}
}
</script>
感謝您的回答。我喜歡這個主意,但是你能解釋一下最後的其他區塊嗎? – TheCorwoodRep
我已經添加了一些關於代碼的評論,如果您需要進一步的解釋,請告知我 – AldoRomo88
我不知道onAuthStateChanged()的返回值。一切都清楚了,謝謝! – TheCorwoodRep