在我的
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中更新的令牌进行道具