我正在尝试制作 Svelte 5 项目(我知道它仍处于 alpha 阶段,但我想玩一下它),我现在使用它作为模板
我的应用程序.svelte
<script>
import { withPrevSignals } from './withPrev'
const color = withPrevSignals('green')
let inputVal = $state('')
function save() {
color.curr = inputVal;
inputVal = ''
}
</script>
<p>Prev: {color.prev}</p>
<p>Curr: {color.curr}</p>
<input bind:value={inputVal} />
<button on:click={save}>Save</button>
<button on:click={color.undo}>Undo</button>
withPrev.js
export function withPrevSignals(initialValue) {
let curr = $state(initialValue)
let prev = $state(undefined)
function undo() {
curr = prev
prev = undefined
}
return {
get curr() { return curr },
set curr(newValue) {
prev = curr
curr = newValue
},
get prev() { return prev },
undo
}
}
我不断收到 Uncaught ReferenceError: $state 在 svelte 5 游乐场和本地均未定义。我在这里做错了什么?