简体   繁体   中英

Svelte - Export a variable but Rollup reports that the variabel isn't exported

I'm new at svelte (3 weeks) and don't now if I'm doing something wrong. Trying to export a const to use in another component, the vscode doesn't report any error, but the rollupJs in the cmd report this error...

[:] Error. 'repos' is not exported by src\components\Request,svelte. imported by src\components\List.svelte

But the variable is been exported just fine (I guees).

this is the Request.svelte

<script>
import {writable} from 'svelte/store'
let userName = ''
export const repos = writable ([]);

and this is the List.svelte whon I want to import the const repos...

<script>
import {onMount} from "svelte";
import {MDCList} from '@material/list';
import {MDCRipple} from '@material/ripple';
import {repos} from './Request.svelte'

repos = data;

So what I'm doing wrong?

If you want to expose a variable for imperative use somewhere else, you need to export it from the module script:

<script context="module">
  import {writable} from 'svelte/store'
  export const repos = writable ([]);
</script>

<script>
  ..
</script>

Tutorial: https://svelte.dev/tutorial/module-exports

However, I see that in your code snippet you do repos = data . This doesn't work, you can't change an exported property like that from the outside. If you want to declaratively change a component property (not a module property) from the outside, you do export let repos =.. (note the let instead of const ) and then do <List repos={data} /> in the component where you use it. Tutorial on props: https://svelte.dev/tutorial/declaring-props

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM