大家好,我正在努力寻找一种方法来在页面刷新后保留我的状态值。目前,我将值存储在本地存储中,并且我尝试了许多解决方案来使用户在页面重新加载后保持登录状态仍然不起作用。每次当我刷新页面时我都会退出,我将非常感谢任何帮助。非常感谢。
Core.ts:
class Core { agv: typeof agv; auth: Auth; workflow: WorkflowApi; graphql: GraphQLSdk; fleetState: Stream<FleetState>; taskList: Stream<TaskList>; zoneList: Stream<ZoneList>; configuration = getConfiguration(); url: string; constructor(options: Options) { const auth = getAuth(options); const graphqlUrl = `${options.host}/graphql`; const graphql = getSdk(new GraphQLClient(graphqlUrl, { fetch: authMiddleware({ auth, fetch: options.fetch }) })); const streamContext = { ...options, headers: getAuthHeaders(auth), getSites: () => auth.session.value?.sites || [], }; this.auth = auth; this.workflow = getWorkflowApi({ auth }) ; this.graphql = graphql; this.fleetState = fleetState.call({ ...options, headers: getAuthHeaders(auth), getSites: () => auth.session.value?.sites || [], }); this.configuration.map.fetch = this.configuration.map.fetch.bind(async () => { const site = auth.session.value.sites[0]; return graphql.map({ site }).then(({ map }) => map); }); this.taskList = taskList.call(streamContext); this.zoneList = zoneList.call(streamContext); this.agv = { ...agv, graphql, map: this.configuration.map, getSites: () => auth.session.value?.sites || [], } as typeof agv; auth.session.listen(session => { window.localStorage.setItem('session', JSON.stringify(session)); console.log(session); if (session) { this.configuration.map.fetch(); } }); } } export { Core };
如果会话持久化到localStorage,如下
那么我认为你可以从
getAuth
中的localStorage进行初始化,如下所示如果没有会话保持在状态,则
null
是表达式的值,并以initialValue
形式返回。