【问题标题】:Fetch new page when user clicks a button using Svelte and Sapper当用户使用 Svelte 和 Sapper 单击按钮时获取新页面
【发布时间】:2020-09-15 10:26:28
【问题描述】:

当用户使用 Svelte 和 Sapper 单击页面底部的按钮时,我被困在获取更多数据的部分。

这是代码。

 <script context="module">
  export function preload(page) {
    return this.fetch(`https://reqres.in/api/users?page=${$count}`) // I know this is not goint to work. but when the button is clicked, I want to fetch page 2 and merge it with page 1 data, show data in ascending order(page1, page2)
    .then(res1 => {
      return res1.json()
    }).then(res2 => {
      return { 
        currentPage: res2.page,
        per_page: res2.per_page,
        notices: res2.data,
        total: res2.total,
        totalPage: res2.total_pages
      }
    })
  }
</script>

<script>
  import { count } from '../../store.js'; // export const count = writable(1); from store.js file
  export let currentPage; // 1
  export let per_page; // 6
  export let notices; 
  export let total; // 12
  export let totalPage; // 2

  const handleClick= () => {
    if ($count < totalPage) {
      count.update(n => n + 1); // update count 1 to 2 and want to deliver changed value to fetch new page
    }
  }
</script>

<main>
  <div id="container">
    <h1 class="cont-tit">Notice Board</h1>

    <div class="noti-list-wrap">
    {#each notices as notice, i}
      <ul class="noti-list">
        <li>
          <a rel=prefetch href={`notice/${notice.id}`}>
            <p class="tit">{`${notice.first_name} ${notice.last_name}`}</p>
            <hr />
            <p class="date">
              {`notice no.${i}`}
            </p>
          </a>
        </li>
      </ul>
    {/each}
      <div class="show-more" on:click={handleClick}>show me more</div>
    </div>
  </div>
</main>

起初我以为我可以使用 $count 来获取第 2 页的数据,但是 script context="module" 中的 import count store 不起作用。

有没有办法将改变的 store 值传递给函数预加载?

【问题讨论】:

    标签: javascript svelte sapper


    【解决方案1】:

    不要尝试在preload 中加载额外的数据。相反,将其视为初始数据 - 它是 - 并以正常方式附加到它。

    这是一个简化版本——它不担心错误处理、竞争条件或 1 以外的初始页面——以大致了解我的意思:

    <script context="module">
      export async function preload(page) {
        const res = await this.fetch('https://reqres.in/api/users?page=1');
        const data = await res.json();
    
        return { 
          currentPage: data.page,
          per_page: data.per_page,
          notices: data.data,
          total: data.total,
          totalPage: data.total_pages
        };
      }
    </script>
    
    <script>
      export let currentPage;
      export let per_page;
      export let notices;
      export let total;
      export let totalPage;
    
      const load_more = async () => {
        currentPage += 1;
        const res = await fetch('https://reqres.in/api/users?page=' + currentPage);
        const data = await res.json();
    
        notices = notices.concat(data.data);
      };
    </script>
    
    <!-- other stuff -->
    {#if currentPage < totalPage}
      <button on:click={load_more}>show me more</button>
    {/if}
    

    【讨论】:

      【解决方案2】:

      我通常也使用快捷方式来更新存储值,如下所示:

      const handleClick= () => {
          if ($count < totalPage) {
            $count = $count + 1); // update count 1 to 2 and want to deliver changed value to fetch new page
          }
        }
      

      我没有看到您基于count 实际获取第 n 页的代码。

      【讨论】:

      • return this.fetch('reqres.in/api/users?page=${$count}') 这部分会根据store的值获取页面api但是this.fetch里面的$count当然不行
      • 如果您不使用模板字符串但'reqres.in/api/users?page=' + $count,它可能会起作用
      • 我也试过了,但错误提示“无法在
      • 我不明白您为什么将预加载函数放在“上下文”脚本中,而不是使用简单的 javascript 文件。无论如何,如果$ 快捷方式不起作用,您仍然可以导入商店并订阅它。然后处理程序可以为新页面进行获取
      • 从我对 sapper 的所有了解来看,我不使用它也许是件好事:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2014-04-10
      • 2018-06-15
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多