Options: Composition
provide
Type:
- provide:
Object | () => Object - inject:
Array<string> | { [key: string]: string | Symbol | Object }
- provide:
Details:
This pair of options are used together to allow an ancestor component to serve as a dependency injector for all its descendants, regardless of how deep the component hierarchy is, as long as they are in the same parent chain. If you are familiar with React, this is very similar to React's
contextfeature.The
provideoption should be an object or a function that returns an object. This object contains the properties that are available for injection into its descendants. You can use ES2015 Symbols as keys in this object, but only in environments that natively supportSymbolandReflect.ownKeys.The
injectoption should be either:- an array of strings, or
- an object where the keys are the local binding name and the value is either:
- the key (string or Symbol) to search for in available injections, or
- an object where:
- the
fromproperty is the key (string or Symbol) to search for in available injections, and - the
defaultproperty is used as fallback value
- the
Note: the
provideandinjectbindings are NOT reactive. This is intentional. However, if you pass down a reactive object, properties on that object do remain reactive.Example:
// parent component providing 'foo' const Provider = { provide: { foo: 'bar' } // ... } // child component injecting 'foo' const Child = { inject: ['foo'], created() { console.log(this.foo) // => "bar" } // ... }With ES2015 Symbols, function
provideand objectinject:const s = Symbol() const Provider = { provide() { return { [s]: 'foo' } } } const Child = { inject: { s } // ... }Using an injected value as the default for a prop:
const Child = { inject: ['foo'], props: { bar: { default() { return this.foo } } } }Using an injected value as data entry:
const Child = { inject: ['foo'], data() { return { bar: this.foo } } }Injections can be optional with default value:
const Child = { inject: { foo: { default: 'foo' } } }If it needs to be injected from a property with a different name, use
fromto denote the source property:const Child = { inject: { foo: { from: 'bar', default: 'foo' } } }Similar to prop defaults, you need to use a factory function for non-primitive values:
const Child = { inject: { foo: { from: 'bar', default: () => [1, 2, 3] } } }See also: Provide / Inject
inject
// TODO
mixins
Type:
Array<Object>Details:
The
mixinsoption accepts an array of mixin objects. These mixin objects can contain instance options like normal instance objects, and they will be merged against the eventual options using the certain option merging logic. For example, if your mixin contains acreatedhook and the component itself also has one, both functions will be called.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, Composition API is now the preferred approach for code reuse between components.
Example:
const mixin = { created() { console.log(1) } } createApp({ created() { console.log(2) }, mixins: [mixin] }) // => 1 // => 2
extends
Type:
ObjectDetails:
Allows one component to extend another, inheriting its component options.
From an implementation perspective,
extendsis almost identical tomixins. The component specified byextendswill be treated as though it were the first mixin.However,
extendsandmixinsexpress different intents. Themixinsoption is primarily used to compose chunks of functionality, whereasextendsis primarily concerned with inheritance.As with
mixins, any options will be merged using the relevant merge strategy.Example:
const CompA = { ... } const CompB = { extends: CompA, ... }