【问题标题】:Error when reading all data from a Svelte store从 Svelte 存储中读取所有数据时出错
【发布时间】:2021-05-05 04:17:49
【问题描述】:

我无法从可写存储中读取所有数据:

Error: 'store' is not exported by src\stores.js, imported by src\components\Users.svelte

商店:

import {subscribe} from 'svelte/internal'
import {writable} from 'svelte/store'
export const fooStore = (key, initial) => {
  const foo = localStorage.getItem(key)
  const data = foo ? JSON.parse(user) : initial
  const store = writable(data, () => {
    const unsubscribe = store.subscribe(value => {
      localStorage.setItem(key, JSON.stringify(value))
    })
    return unsubscribe
  })
  return store
}

观点:

<script>
  import {store} from '../stores.js'
</script>

<table>
  <thead></thead>
  <tbody>
    {#each $store as foo}
      <tr>
        <td>foo.bar</td>
        <td>foo.qaz</td>
      </tr>
    {/each}
  </tbody>
</table>

【问题讨论】:

    标签: svelte-3


    【解决方案1】:

    您从store 文件中导出的唯一内容是一个名为fooStore 的函数。

    当您执行import { store } from ... 时,它会在该文件中查找名为store命名导出,因此它会期望该文件在某处具有@987654324 @ 或类似名称。

    您可以在 store 文件中声明一些商店,如果您愿意,可以使用 fooStore 函数创建它们:

    
    export const fooStore = () => {}
    
    // with fooStore
    export const myStore1 = fooStore();
    
    // plain store
    export const myStore2 = writable(123);
    

    或者你在你的组件中导入函数并在那里声明商店

    <script>
      import { fooStore } from '...'
      const store = fooStore(...)
    </script>
    

    【讨论】:

    • 那么我如何通知 Svelte 我的可写存储实际上是一个数组,在这种情况下是一个大数组
    • 你只是在你的商店里放了一个数组。
    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 2021-09-18
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多