Skip to content

Commit 5eff188

Browse files
authored
Merge pull request #701 from sveltejs/gh-699
alias #component in hoisted event handlers
2 parents b3dc57d + 2131b28 commit 5eff188

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/generators/dom/visitors/Element/EventHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function visitEventHandler(
4545
const declarations = usedContexts.map(name => {
4646
if (name === 'state') {
4747
if (shouldHoist) state.usesComponent = true;
48-
return `var state = #component.get();`;
48+
return `var state = ${block.alias('component')}.get();`;
4949
}
5050

5151
const listName = block.listNames.get(name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
data: {
3+
foo: [1],
4+
a: 42
5+
},
6+
7+
html: `
8+
<button>click me</button>
9+
`,
10+
11+
test (assert, component, target, window) {
12+
const button = target.querySelector('button');
13+
const event = new window.MouseEvent('click');
14+
15+
button.dispatchEvent(event);
16+
assert.equal(component.snapshot, 42);
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{#each foo as bar}}
2+
<button on:click='baz(a)'>click me</button>
3+
{{/each}}
4+
5+
<script>
6+
export default {
7+
methods: {
8+
baz(a) {
9+
this.snapshot = a;
10+
}
11+
}
12+
};
13+
</script>

0 commit comments

Comments
 (0)