8000 2.5: inject default values · vuejs/v2.vuejs.org@31f6f92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31f6f92

Browse files
committed
2.5: inject default values
1 parent 2b147c7 commit 31f6f92

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/v2/api/index.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ type: api
976976
977977
- **Type:**
978978
- **provide:** `Object | () => Object`
979-
- **inject:** `Array<string> | { [key: string]: string | Symbol }`
979+
- **inject:** `Array<string> | { [key: string]: string | Symbol | Object }`
980980
981981
- **Details:**
982982
@@ -1055,6 +1055,42 @@ type: api
10551055
}
10561056
```
10571057
1058+
> In 2.5.0+ injections can be optional with default value:
1059+
1060+
``` js
1061+
const Child = {
1062+
inject: {
1063+
foo: { default: 'foo' }
1064+
}
1065+
}
1066+
```
1067+
1068+
If it needs to be injected from a property with a different name, use `from` to denote the source property:
1069+
1070+
``` js
1071+
const Child = {
1072+
inject: {
1073+
foo: {
1074+
from: 'bar',
1075+
default: 'foo'
1076+
}
1077+
}
1078+
}
1079+
```
1080+
1081+
Similar to prop defaults, you need to use a factory function for non primitive values:
1082+
1083+
``` js
1084+
const Child = {
1085+
inject: {
1086+
foo: {
1087+
from: 'bar',
1088+
default: () => [1, 2, 3]
1089+
}
1090+
}
1091+
}
1092+
```
1093+
10581094
## Options / Misc
10591095
10601096
### name

0 commit comments

Comments
 (0)
0