Skip to content

Commit 48643ca

Browse files
authored
Merge pull request #1280 from sveltejs/dynamic-component-mount-ref
when mounting dynamic components, set ref if required
2 parents 6a15ebd + 6ff60db commit 48643ca

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/generators/nodes/Component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,12 @@ export default class Component extends Node {
307307
);
308308
}
309309

310-
block.builders.mount.addLine(
311-
`if (${name}) ${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
312-
);
310+
block.builders.mount.addBlock(deindent`
311+
if (${name}) {
312+
${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});
313+
${ref && `#component.refs.${ref.name} = ${name};`}
314+
}
315+
`);
313316

314317
const updateMountNode = this.getUpdateMountNode(anchor);
315318

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
html: `Foo`,
3+
4+
test(assert, component) {
5+
assert.ok(component.refs.test);
6+
}
7+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<:Component {foo} ref:test/>
2+
3+
<script>
4+
import Foo from './Foo.html';
5+
6+
export default {
7+
data() {
8+
return { foo: Foo };
9+
}
10+
};
11+
</script>

0 commit comments

Comments
 (0)