I 存储在可写存储中的值
import { writable } from 'svelte/store'
export const user = writable(null)
export const isLoggedIn = writable(false)
Run Code Online (Sandbox Code Playgroud)
然后我导入这些值并将它们设置在索引中
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { user, isLoggedIn } from "../stores/authStore";
const provider = new GoogleAuthProvider();
const auth = getAuth();
function signIn() {
signInWithPopup(auth, provider)
.then((result) => {
user.set(result.user)
isLoggedIn.set(true);
console.log($isLoggedIn);
console.log($user.email);
if ($user.email.includes(".edu")) window.location.href = "/home";
else window.location.href = "/Sorry";
//sign user into db
})
.catch((error) => {
console.log("Someting wrong");
console.error(error);
});
}
Run Code Online (Sandbox Code Playgroud)
当我更改页面并使用再次打印值时
console.log($userValue, $isLoggedIn) …Run Code Online (Sandbox Code Playgroud)