3

Just a basic question: Is the $-syntax for stores applicable in non-component JavaScript files?

The doc says:

Any time you have a reference to a store, you can access its value inside a component by prefixing it with the $ character.

However, this official example seems to use the $-syntax in a derived store which is not a component:

export const elapsed = derived(
    time,
    $time => Math.round(($time - start) / 1000)
);

Is this a special case for custom stores? Or is it possible because it gets imported into a component?

robsch
  • 9,358
  • 9
  • 63
  • 104

1 Answers1

4

The answer is no, because only Svelte files will be compiled.

And you are right about the derived store. But this is only to make clear the callback receives the value and not the subscription. You can use other value names as well and you do not need to start with a $.

export const elapsed = derived(
    time,
    _time => Math.round((_time - start) / 1000)
);
voscausa
  • 11,253
  • 2
  • 39
  • 67
  • The import of a JS file into a component does not mean that it gets compiled as well? – robsch May 20 '21 at 12:10
  • No. But of course you can use store.subscribe() or use get(store) here to get the currenty store value. Or pass the $store as a function argument to an imported js function. Imported from a js file. – voscausa May 20 '21 at 12:27