Skip to content

Document removal of $children API on Migration guide #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/guide/migration/children.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
badges:
- removed
---

# $children <MigrationBadges :badges="$frontmatter.badges" />

## Overview

`$children` instance property removed from Vue 3.0 and no longer supported.

## 2.x Syntax

In 2.x, developers could access direct child components of the current instance with `this.$children`:

```html
<div ref="app" id="app">
<img alt="Vue logo" src="./assets/logo.png" width="25%" />
<my-button>Change logo</my-button>
</div>
```

```js
export default {
name: "App",
components: {
MyButton,
},
mounted() {
console.log(this.$children); // [VueComponent]
},
};
</script>
```

## 3.x Update

In 3.x, `$children` property is removed and no longer supported. Instead, if you need to access a child component instance, we recommend using [$refs](/guide/component-template-refs.html#template-refs).
Copy link
Member

@LinusBorg LinusBorg Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refs only work in a straightforward way when accessing children in a component's own template.

However, I think there are quite a few plugins out there that rely on $children in order to access child components in slots - think of a compound component like this:

<tabs>
  <tab  name="B" />
  <tab name="B" />
</tabs>

It's not a particularly good pattern in my opinion as you could easily end up with children that you don't expect, but it seems it's used in the community.

Can we provide some guidance how to avoid such a pattern?

  • One way would be to provide a ref function (like for v-for) through v-slot props, but that's not really ergonomic.
  • Provide/inject can be used to make children register themselves with the parent.
    • that pattern could even be wrapped in a mixin /comp-Function to simulate $childrenbehaviour

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LinusBorg, I agree with the addition but (as you mentioned in he above comment) let's add it in the follow-up PR 👍

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love to see guidance on this pattern please

3 changes: 2 additions & 1 deletion src/guide/migration/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ The following consists a list of breaking changes from 2.x:
### Removed APIs

- [`keyCode` support as `v-on` modifiers](/guide/migration/keycode-modifiers.html)
- [$on, $off and \$once instance methods](/guide/migration/events-api.html)
- [$on, $off and $once instance methods](/guide/migration/events-api.html)
- [Filters](/guide/migration/filters.html)
- [Inline templates attributes](/guide/migration/inline-template-attribute.html)
- [`$children` intance property](/guide/migration/children.md)
- `$destroy` instance method. Users should no longer manually manage the lifecycle of individual Vue components.

## Supporting Libraries
Expand Down