W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
如果 store可寫入的(即具有set
方法),則可以綁定其值,就像可以綁定局部組件狀態(tài)一樣。
在此示例中,我們有一個(gè)可寫 store
:name
和一個(gè)派生store
:greeting
,嘗試更改<input>
標(biāo)簽:
<input bind:value={$name}>
現(xiàn)在,更改name
的輸入值 ,其值和依賴項(xiàng)都將獲得更新。
我們還可以直接分配store
值在組件內(nèi)部,嘗試添加<button>
標(biāo)簽:
<button on:click="{() => $name += '!'}">
Add exclamation mark!
</button>
此處 $name += '!'
相當(dāng)于 name.set($name + '!')
。
示例代碼
<script>
import { name, greeting } from './stores.js';
</script>
<h1>{$greeting}</h1>
<input value={$name}>
import { writable, derived } from 'svelte/store';
export const name = writable('world');
export const greeting = derived(
name,
$name => `Hello ${$name}!`
);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: