Ads by ProfitSence
Close

Access Svelte store value from external javascript/typescript files.

Last Updated on Wednesday 5th Oct 2022

Accessing Svelte store in javascript

You can't just access the store value with the ($) sign in any javascript or typescript file.

  • If you just want to know the current value, Svelte has a helper for that, the get function
			
					import { get } from "svelte/store";
import { data } from "$lib/stores";

let getData = get(data);

			
	

In Svelte Files we can access with ($) sign

			
					import { data } from "$lib/stores";

let getData = $data;

			
	
  • You Can also subscribe to store to know the value
			
					import { get } from "svelte/store";
import { data } from "$lib/stores";

data.subscribe(value => {
  // blah blah blah
})

			
	

Useful Video related to this Topic

Youtube Video

Kudos to lihautan

svelte typescript
svelte store typescript
svelte import external js
svelte stores
typescript svelte store
svelte stores typescript
svelte store
svelte/store
svelte external js
svelte typescript store