Разве мы не можем передавать данные obj в качестве опоры различным компонентам в svelte? - PullRequest
0 голосов
/ 16 июня 2020

Итак, я хочу передать данные (объект) в качестве опоры другому компоненту. Моя цель - сделать компонент таблицы многоразовым.

App.svelte

<script>
import {onMount} from 'svelte';
import Tabel from './components/Tabel.svelte';
let data = [];
onMount(async () => {
  try {
   let res = await fetch(url); // your url
   data = await res.json();
  } catch(err){
   throw err;
  }
})
</script>

<Tabel {data} />

Table.svelte

<script>
export let data = [];
</script>
<table class="table-auto">
  <thead>
    <tr>
      <th class="px-4 py-2">Name</th>
      <th class="px-4 py-2">Age</th>
      <!-- and so on -->
    </tr>
  </thead>
  <tbody>
  {#each data as {name, age}, i}
    <tr>
      <th class="px-4 py-2">{name}</th>
      <th class="px-4 py-2">{age}</th>
      <!-- and so on -->
    </tr>
  {/each}
  </tbody>
</table>

Но у меня такая ошибка:

rollup v2.16.1
bundles src/main.js → public/build/bundle.js...
[!] Error: Could not resolve './components/Tabel.svelte' from src/App.svelte
Error: Could not resolve './components/Tabel.svelte' from src/App.svelte

1 Ответ

1 голос
/ 16 июня 2020

У вас опечатка. Импортировать таблицу по ее собственному имени:

<script>
import Table from './components/Table.svelte';  // <- fix this

//...
</script>


<Table {data}/>
...