>  Q&A  >  본문

페이지 새로 고침 후 상태를 변경하지 않고 유지하는 방법은 무엇입니까?

안녕하세요 여러분, 저는 페이지를 새로 고친 후에도 상태 값을 유지하는 방법을 찾으려고 노력 중입니다. 현재 로컬 저장소에 값을 저장하고 있으며 페이지를 다시 로드한 후에도 사용자 로그인을 유지하기 위해 많은 솔루션을 시도했지만 여전히 작동하지 않습니다. 페이지를 새로 고칠 때마다 이 작업이 종료됩니다. 도움을 주시면 대단히 감사하겠습니다. 매우 감사합니다.

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 };

P粉384244473P粉384244473236일 전424

모든 응답(1)나는 대답할 것이다

  • P粉323374878

    P粉3233748782024-01-30 00:40:23

    세션이 localStorage에 지속되는 경우 다음과 같이

    으아아아

    그럼 아래와 같이 getAuth에서 localStorage를 초기화할 수 있을 것 같아요

    으아아아

    상태에 세션이 남아 있지 않으면 null 是表达式的值,并以 initialValue양식이 반환됩니다.

    회신하다
    0
  • 취소회신하다