mirror of https://github.com/sveltejs/svelte
closes #11116 closes #11339runes-in-constructor
parent
f2f71ae3a1
commit
d45c8f0662
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
feat: allow state and derived declarations inside class constructors
|
@ -0,0 +1,31 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
solo: true,
|
||||
html: `
|
||||
<button>0</button>
|
||||
<button>0</button>
|
||||
`,
|
||||
|
||||
async test({ assert, target }) {
|
||||
const [btn1, btn2] = target.querySelectorAll('button');
|
||||
|
||||
await btn1?.click();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>1</button>
|
||||
<button>0</button>
|
||||
`
|
||||
);
|
||||
|
||||
await btn2?.click();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>2</button>
|
||||
<button>1</button>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,24 @@
|
||||
<script>
|
||||
class Counter {
|
||||
#count;
|
||||
get secretCount() {
|
||||
return this.#count;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.#count = $state(0); // TODO
|
||||
this.count = $state(0);
|
||||
this.double = $derived(this.count * 2);
|
||||
}
|
||||
|
||||
increment() {
|
||||
this.#count++;
|
||||
this.count++;
|
||||
}
|
||||
}
|
||||
|
||||
const counter = new Counter();
|
||||
</script>
|
||||
|
||||
<button on:click={() => counter.count++}>{counter.count}</button>
|
||||
<button on:click={counter.increment}>{counter.secretCount}</button>
|
Loading…
Reference in new issue