Skip to content

Commit

Permalink
Merge pull request #1280 from sveltejs/dynamic-component-mount-ref
Browse files Browse the repository at this point in the history
when mounting dynamic components, set ref if required
  • Loading branch information
Rich-Harris authored Mar 27, 2018
2 parents 6a15ebd + 6ff60db commit 48643ca
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,12 @@ export default class Component extends Node {
);
}

block.builders.mount.addLine(
`if (${name}) ${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
);
block.builders.mount.addBlock(deindent`
if (${name}) {
${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});
${ref && `#component.refs.${ref.name} = ${name};`}
}
`);

const updateMountNode = this.getUpdateMountNode(anchor);

Expand Down
1 change: 1 addition & 0 deletions test/runtime/samples/dynamic-component-ref/Foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
7 changes: 7 additions & 0 deletions test/runtime/samples/dynamic-component-ref/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
html: `Foo`,

test(assert, component) {
assert.ok(component.refs.test);
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/dynamic-component-ref/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<:Component {foo} ref:test/>

<script>
import Foo from './Foo.html';

export default {
data() {
return { foo: Foo };
}
};
</script>

0 comments on commit 48643ca

Please sign in to comment.