File tree 2 files changed +31
-2
lines changed
2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 7
7
nextTick ,
8
8
Ref ,
9
9
readonly ,
10
- reactive
10
+ reactive ,
11
+ defineComponent
11
12
} from '../src/index'
12
13
import { render , nodeOps , serialize } from '@vue/runtime-test'
13
14
@@ -91,6 +92,34 @@ describe('api: provide/inject', () => {
91
92
expect ( serialize ( root ) ) . toBe ( `<div>foobar</div>` )
92
93
} )
93
94
95
+ it ( 'bound to instance' , ( ) => {
96
+ const Provider = {
97
+ setup ( ) {
98
+ return ( ) => h ( Consumer )
99
+ }
100
+ }
101
+
102
+ const Consumer = defineComponent ( {
103
+ name : 'Consumer' ,
104
+ inject : {
105
+ foo : {
106
+ from : 'foo' ,
107
+ default ( ) {
108
+ return this ! . $options . name
109
+ }
110
+ }
111
+ } ,
112
+ render ( ) {
113
+ // @ts -ignore
114
+ return this . foo
115
+ }
116
+ } )
117
+
118
+ const root = nodeOps . createElement ( 'div' )
119
+ render ( h ( Provider ) , root )
120
+ expect ( serialize ( root ) ) . toBe ( `<div>Consumer</div>` )
121
+ } )
122
+
94
123
it ( 'nested providers' , ( ) => {
95
124
const ProviderOne = {
96
125
setup ( ) {
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ export function inject(
60
60
return provides [ key as string ]
61
61
} else if ( arguments . length > 1 ) {
62
62
return treatDefaultAsFactory && isFunction ( defaultValue )
63
- ? defaultValue ( )
63
+ ? defaultValue . call ( instance . proxy )
64
64
: defaultValue
65
65
} else if ( __DEV__ ) {
66
66
warn ( `injection "${ String ( key ) } " not found.` )
You can’t perform that action at this time.
0 commit comments