P粉1954022922023-08-29 21:23:43
我不确定您尝试过什么,但我知道这会起作用。当然,您可以将 onAuthStateChanged 移出您的商店,它仍然可以工作。请记住,您必须使用观察者或计算道具来跟踪 store.user 并更新 UI。
import { getAuth, onAuthStateChanged } from 'firebase/auth'; const auth = getAuth(); onAuthStateChanged(auth, async () => { const store = useStore(); store.setUser(); }); const useStore = defineStore('store', { state: () => ({ user: null }), actions: { setUser() { // Set user here // this.user = ... } } });