javascript - Svelte - import const from component does not work - Stack Overflow

I try to import a const value from a Svelte ponent, but rollup says, the ponent does not export this va

I try to import a const value from a Svelte ponent, but rollup says, the ponent does not export this value. What do I wrong, or is it a rollup problem ?

related REPL

Component.svelte:

<script>
export const answer = 42;
</script>

I try to import a const value from a Svelte ponent, but rollup says, the ponent does not export this value. What do I wrong, or is it a rollup problem ?

related REPL

Component.svelte:

<script>
export const answer = 42;
</script>

App.svelte:

<script>
import { answer } from './Component.svelte';
</script>

<h1>{answer}</h1>

The same problem appears when importing an enum definition.

Share Improve this question asked Dec 25, 2020 at 17:58 maideasmaideas 731 silver badge5 bronze badges 2
  • 1 try replacing <script> with <script context="module"> in Component.svelte. I have added this as a answer below. – A Paul Commented Dec 25, 2020 at 18:11
  • more about it in here: svelte.dev/tutorial/module-exports – kibe Commented Dec 25, 2020 at 18:12
Add a ment  | 

2 Answers 2

Reset to default 7

Svelte use the export syntax to define a ponent props. So if you want to use this export like you do in modern javascript modules you have to indicate it to the Svelte piler using context="module" like:

<script context="module">
    export const answer = 42;
</script>

Checkout the REPL and the doc to learn a little more about it.

Try replacing with in Component.svelte. But please note it’ll be read-only no matter how you define it (const or let), so if you want to change the value you may want to create setter or getter function to do that and then access the variable using that.

 <script context="module">
    export const answer = 42;
 </script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743766550a4503583.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信