首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nuxt中使用vue-apollo

在nuxt中使用vue-apollo
EN

Stack Overflow用户
提问于 2019-09-04 10:52:10
回答 1查看 1.1K关注 0票数 1

我正在尝试使用apollo-cache-persist,但是我被文档:https://github.com/apollographql/apollo-cache-persist/blob/master/README.md#web弄糊涂了。

这是web初始化代码:

代码语言:javascript
复制
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';

const cache = new InMemoryCache({...});

// await before instantiating ApolloClient, else queries might run before the cache is persisted
await persistCache({
  cache,
  storage: window.localStorage,
});

await persistCache()抛出了一个错误,我不明白如果没有异步它怎么工作。我猜我需要把它放在一个插件中,但我也不太确定该怎么做。

有关阿波罗客户端配置的更多信息,请参阅另一个问题:@client Apollo GQL tag breaks query

EN

回答 1

Stack Overflow用户

发布于 2019-12-01 15:17:44

我在nuxt.js上不是最新的。

看看vue-apollo作者的vue-cli-plugin-ssr吧。它正在修改main.js,通过用export async function createApp包装new Vue(...)和beforeApp,afterApp异步回调,以恢复服务器发送的缓存。您可以只使用vue add @akryum/ssr,在考虑了合并客户端和服务器缓存的适当方式后,您将在entry-client.js中找到一个放置异步钩子的位置……

或者你可以做一些更简单的事情:

代码语言:javascript
复制
// vue-apollo.js
import { persistCache } from 'apollo-cache-persist'
export async function willCreateProvider() {
  await persistCache({ cache, storage: window.localStorage })
}

export function createProvider(options = {}) { }
代码语言:javascript
复制
// main.js
import { createProvider, willCreateProvider } from './vue-apollo'
willCreateProvider().then(() => {
  new Vue({
    router,
    apolloProvider: createProvider(),
    render: h => h(App),
  }).$mount('#app')
})

apollo-cache-persist-dev@^0.2.0中也有persistCacheSync

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57780887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档