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 = ... } } });