Skip to content

Commit

Permalink
1.2.3 変数とイベントハンドラによる「カートに入れる」ボタンの挙動作成
Browse files Browse the repository at this point in the history
  • Loading branch information
tenkao committed May 9, 2023
1 parent 483ebad commit 8086783
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/App.svelte
@@ -1,3 +1,13 @@
<script>
// カートに入っている商品IDの配列
let cart = []
// カートに商品を追加する関数
function addToCart(productId) {
cart = [...cart, productId]
}
</script>

<header class="header">
<a href="/" class="header-title">Svelte EC</a>
<nav>
Expand All @@ -22,7 +32,11 @@
<dd>3,500円</dd>
</dl>
<div>
<button>カートに入れる</button>
{#if !cart.includes('svelte-book')}
<button on:click={() => addToCart('svelte-book')}>カートに入れる</button>
{:else}
<button disabled>カート追加済み</button>
{/if}
</div>
</div>
</div>
Expand Down

0 comments on commit 8086783

Please sign in to comment.