面试题答案
一键面试<script>
import { writable, derived } from'svelte/store';
// 定义购物车商品数量store
const cartQuantity = writable(5);
// 定义商品单价store
const itemPrice = writable(10);
// 衍生store计算购物车商品总价格
const totalPrice = derived([cartQuantity, itemPrice], ([$cartQuantity, $itemPrice]) => {
return $cartQuantity * $itemPrice;
});
</script>
<div>
<p>购物车商品总价格: {$totalPrice}</p>
</div>