robsch
robsch

Reputation: 9728

Does Svelte store's auto-subscription work in non-component files?

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?

Upvotes: 3

Views: 1088

Answers (1)

voscausa
voscausa

Reputation: 11706

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)
);

Upvotes: 5

Related Questions