我正在尝试https://github.com/FormidableLabs/urql/tree/master/packages/svelte-urql伙计们完成的令人惊叹的工作。
直到今天的问题,一切都很好。
我正在使用下面的代码,它给了我这个错误:
Error: Function called outside component initialization
at get_current_component (index.mjs:615)
at getContext (index.mjs:648)
at getClient (urql-svelte.mjs:55)
at query (urql-svelte.mjs:81)
at Players.svelte:41
代码:
<script>
import { query } from '@urql/svelte'
import { myQuery } from './myQuery'
let players
let myVars
function sleep (ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
$: (async () => {
await sleep(2000) // this gives me the error; removing it make it work
players = query({
query: myQuery,
variables: { ...myVars },
requestPolicy: 'cache-and-network'
})
})()
</script>
{#if !players}
Loading players...
{:else}
Players loaded! Do the work.
{/if}
你能提出什么问题吗?
如果我使用它就可以await()
了onMount()
!像这样:
onMount(async () => {
await sleep(2000)
loaded = true
})
这里的代码@urql/svelte
:
- query.ts:https ://github.com/FormidableLabs/urql/blob/master/packages/svelte-urql/src/operations/query.ts
- context.ts:https ://github.com/FormidableLabs/urql/blob/master/packages/svelte-urql/src/context.ts
也许是context
代码?
import { setContext, getContext } from 'svelte';
import { Client, ClientOptions } from '@urql/core';
const CLIENT = '$$_URQL';
export const getClient = (): Client => getContext(CLIENT);
export const setClient = (client: Client): void => {
setContext(CLIENT, client);
};
export const initClient = (args: ClientOptions): Client => {
const client = new Client(args);
setClient(client);
return client;
};
如果您需要,我可以在 CodeSandbox 上创建一个 REPL,没问题。
错误@urql/svelte
:https://github.com/FormidableLabs/urql/issues/795。
有关您的 Svelte 项目的信息: - Chrome 83 - Svelte 版本:3.23.0 - 汇总