我不確定您嘗試過(guò)什麼,但我知道這會(huì)起作用。當(dāng)然,您可以將 onAuthStateChanged 移出您的商店,它仍然可以工作。請(qǐng)記住,您必須使用觀(guān)察者或計(jì)算道具來(lái)追蹤 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 = ... } } });