代码之家  ›  专栏  ›  技术社区  ›  pmiranda

ReactJS和Apollo客户端,加载应用程序。设置localStorage项后的js

  •  0
  • pmiranda  · 技术社区  · 4 年前

    在我的 App.js (应用程序的基本组件)我有 Apollo Client 提供程序,它需要从中获取一些令牌 localStorage .

    const authLink = setContext((_, { headers }) => {
      const token = localStorage.getItem('access_token');
      return {
        headers: {
          ...headers,
          authorization: token ? `Bearer ${token}` : ''
        }
      };
    });
    
    const client = new ApolloClient({
      cache: new InMemoryCache({
        addTypename: false
      }),
      link: authLink.concat(httpLink)
    });
    
    ...
    class App extends Component {
      render() {
        return (
          <ApolloProvider client={client}>
            <Router />
          </ApolloProvider>
            
    

    但在某些情况下,该令牌将根据一些事件在localStorage中设置,但我不知道如何通过 应用程序。js 再次是为了让阿波罗供应商 client 使用localStorage中更新的令牌进行道具

    0 回复  |  直到 4 年前
    推荐文章