Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Is it possible to access Svelte store from external js files?

$
0
0

I am wondering if i would be able to access my Svelte store values from a plain .js file.

I am trying to write functions returning a dynamic value based on a store value, to import them in any component.But in a plain .js file I can't just access the store value with the $ sign..

Quick exemple of a basic function that uses a store value and could be used on multiple components:

//in .sveltefunction add() {    $counter = $counter + 1;}

EDIT: rephrasing a bit

EDIT:Found a solution but i don't really know if it's really optimized..

//in .js fileimport { get } from "svelte/store";import { counter } from "./stores";export function add() {    var counterRef = get(counter);    counter.set(counterRef + 1);}

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>