Skip to content

Update the API entries for mixins and extends to reference setup #1129

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

Closed
Closed
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
48 changes: 43 additions & 5 deletions src/api/options-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

Mixin hooks are called in the order they are provided, and called before the component's own hooks.

:::info
In Vue 2, mixins were the primary mechanism for creating reusable chunks of component logic. While mixins continue to be supported in Vue 3, the [composition API](/guide/composition-api-introduction.html) is now the preferred approach for code reuse between components.
:::

- **Example:**

```js
const mixin = {
created: function() {
created() {
console.log(1)
}
}
Expand All @@ -34,26 +38,60 @@

## extends

- **Type:** `Object | Function`
- **Type:** `Object`

- **Details:**

Allows declaratively extending another component (could be either a plain options object or a constructor). This is primarily intended to make it easier to extend between single file components.
Allows one component to extend another, inheriting its component options.

From an implementation perspective, `extends` is almost identical to `mixins`. The component specified by `extends` will be treated as though it were the first mixin.

However, `extends` and `mixins` express different intents. The `mixins` option is primarily used to compose chunks of functionality, whereas `extends` is primarily concerned with inheritance.

This is similar to `mixins`.
As with `mixins`, any options will be merged using the relevant merge strategy.

- **Example:**

```js
const CompA = { ... }

// extend CompA without having to call `Vue.extend` on either
const CompB = {
extends: CompA,
...
}
```

- **Usage with `setup`:**

Using `extends` loses the explicitness of `setup`, so combining the two approaches is usually undesirable. However, it can be useful in cases where the extending component contains very little code and is almost identical to the extended component.

`extends` will not inherit the `setup` function from the extended component but it can be copied across manually:

```js
const CompA = { ... }

const CompB = {
extends: CompA,
setup: CompA.setup,
...
}
```

If you need to add your own `setup` function you can call the inherited `setup` function at an appropriate point:

```js
const CompA = { ... }

const CompB = {
extends: CompA,
setup (props, context) {
const inheritedProperties = CompA.setup(props, context)
...
},
...
}
```

## provide / inject

- **Type:**
Expand Down