我有一个从 graphql 服务器获取帖子的组件。如果用户位于页面底部,它将获取更多帖子。我怎样才能检查他是否达到了它?我查看了clientHeight、innerHeight 和outerHeight,但只有clientHeight 才有意义。Probalby 我必须从中减去一些东西,但我不知道究竟是什么以及我错过了什么?
<script>
import {getClient, query} from 'svelte-apollo';
import ContentWrapper from '../layout/ContentWrapper.svelte';
import {GET_POSTS} from "../services/posts/postsApi";
let pageInfo;
let first = 2;
let after;
const client = getClient();
let posts = query(client, {query: GET_POSTS, variables: {first}});
const fetchMore = () => {
posts.fetchMore({
variables: {first, after},
updateQuery: (previousResult, {fetchMoreResult}) => {
const newEdges = fetchMoreResult.getPosts.edges;
const pageInfo = fetchMoreResult.getPosts.pageInfo;
return newEdges.length
? {
getPosts: {
__typename: previousResult.getPosts.__typename,
edges: [...previousResult.getPosts.edges, ...newEdges],
pageInfo
}
}
: previousResult;
}
})
};
const setState = queryData => {
pageInfo = queryData.getPosts.pageInfo;
after = pageInfo.endCursor;
return '';
};
let y;
let clientHeight;
const checkScrollPosition = () => {
if(clientHeight <= y)
fetchMore();
}
</script>
<svelte:window on:scroll={checkScrollPosition} bind:scrollY={y} />
<ContentWrapper>
<div slot="header">
<h4>Page of Pagination</h4>
</div>
<div slot="body" bind:clientHeight={clientHeight}>
{#await $posts}
<h3>Loading...</h3>
{:then result}
{setState(result.data)}
{#each result.data.getPosts.edges as post}
<div class="card">
<div class="card-content">
<p class="card-text">{post.node.body}</p>
</div>
</div>
{/each}
<div id="paginator">
{#if pageInfo}
{#if pageInfo.hasNextPage}
<div class="btn" on:click={fetchMore}>Next</div>
{/if}
{/if}
</div>
{:catch error}
{console.log(error)}
{/await}
</div>
</ContentWrapper>