user in the Svelte store, which gets updated in the Login component. The top-level App component uses this to check user permissions and adjust routing accordingly. The App component never gets a new value for user even after the Login component updates it. Code below:
stores.js
import { writable } from 'svelte/store'; export const user = writable(null); Login Component
import { user } from '../stores.js'; function login(callback) { //...handles login, callback upon login success } function accountReceived(account) { if (account) { user.set({ name: account.name, email: account.username }); } else { user.set(null); } } onMount(() => login(accountReceived)); App Component
import { user } from './stores.js'; import router from 'page'; router('/admin', (ctx, next) => { if ($user !== null) { // this is ALWAYS null //...redirect to login } else { next(); }, () => { //..load admin page } }); //tried this function isLoggedIn() { return $user !== null; // this is also ALWAYS null } //and this $: isLoggedInVar = user !== null; // this is also ALWAYS null //also tried this let isLoggedInVar2 = $user !== null; // this is ALWAYS null //also tried using explicit subscription let isLoggedInVar3 = false; const unsubscribe = user.subscribe(val => { isLoggedInVar3 = val !== null; // this is also ALWAYS null }); https://stackoverflow.com/questions/67223057/why-isnt-this-svelte-store-value-updating April 23, 2021 at 10:06AM
没有评论:
发表评论