Getting started
Supported Tauri Version
This plugin requires Tauri 2.0 or later.
Install
Install the Rust crate by adding the following to your Cargo.toml
file:
toml
[dependencies]
tauri-plugin-svelte = "2"
Install the JavaScript package with your preferred package manager:
shell
npm install @tauri-store/svelte
shell
pnpm add @tauri-store/svelte
shell
deno add npm:@tauri-store/svelte
shell
bun add @tauri-store/svelte
shell
yarn add @tauri-store/svelte
Usage
- Enable the required permissions in your capabilities file:
json
{
"identifier": "svelte",
"windows": ["*"],
"permissions": ["svelte:default", "core:event:default"]
}
- Register the plugin with Tauri:
rust
tauri::Builder::default()
.plugin(tauri_plugin_svelte::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
- Create a store:
typescript
import { Store } from '@tauri-store/svelte';
export const store = new Store('counter', { counter: 0 });
- Start it:
typescript
await store.start();
TIP
Stores won't be saved nor synchronized until you call the start method.
- Use the store in your Svelte components:
svelte
<script>
import { store } from '$lib/stores/counter';
</script>
<div>
<p>Counter: {$store.counter}</p>
<button type="button" onclick={() => $store.counter++}>
<span>Increment</span>
</button>
</div>
Runes
You can also use runes instead of conventional stores.
typescript
import { RuneStore } from '@tauri-store/svelte';
const store = new RuneStore('counter', { counter: 0 });
function increment() {
// "state" is reactive!
store.state.counter += 1;
}