Skip to content

Commit 79b448d

Browse files
committed
update inject api docs for factory usage
close vuejs#1809, ref vuejs/core#6194
1 parent e602b1d commit 79b448d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/api/composition-api-dependency-injection.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ Injects a value provided by an ancestor component or the application (via `app.p
6666

6767
The first argument is the injection key. Vue will walk up the parent chain to locate a provided value with a matching key. If multiple components in the parent chain provides the same key, the one closest to the injecting component will "shadow" those higher up the chain. If no value with matching key was found, `inject()` returns `undefined` unless a default value is provided.
6868

69-
The second argument is optional and is the default value to be used when no matching value was found. It can also be a factory function to return values that are expensive to create. If the default value is a function, then `false` must be passed as the third argument to indicate that the function should be used as the value instead of the factory.
69+
The second argument is optional and is the default value to be used when no matching value was found.
70+
71+
The second argument can also be a factory function that returns values that are expensive to create. In this case, `true` must be passed as the third argument to indicate that the function should be used as a factory instead of the value itself.
7072

7173
Similar to lifecycle hook registration APIs, `inject()` must be called synchronously during a component's `setup()` phase.
7274

@@ -93,11 +95,11 @@ Injects a value provided by an ancestor component or the application (via `app.p
9395
// inject with default value
9496
const bar = inject('foo', 'default value')
9597
96-
// inject with default value factory
97-
const baz = inject('foo', () => new Map())
98+
// inject with function default value
99+
const fn = inject('function', () => {})
98100
99-
// inject with function default value, by passing the 3rd argument
100-
const fn = inject('function', () => {}, false)
101+
// inject with default value factory
102+
const baz = inject('factory', () => new ExpensiveObject(), true)
101103
</script>
102104
```
103105

0 commit comments

Comments
 (0)