File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed
test/unit/features/options Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 1
1
/* @flow */
2
2
3
3
import { hasSymbol } from 'core/util/env'
4
+ import { warn } from '../util/index'
4
5
import { defineReactive } from '../observer/index'
5
6
6
7
export function initProvide ( vm : Component ) {
@@ -30,7 +31,18 @@ export function initInjections (vm: Component) {
30
31
let source = vm
31
32
while ( source ) {
32
33
if ( source . _provided && provideKey in source . _provided ) {
33
- defineReactive ( vm , key , source . _provided [ provideKey ] )
34
+ if ( process . env . NODE_ENV !== 'production' ) {
35
+ defineReactive ( vm , key , source . _provided [ provideKey ] , ( ) => {
36
+ warn (
37
+ `Avoid mutating a injections directly since the value will be ` +
38
+ `overwritten whenever the provided component re-renders. ` +
39
+ `injections being mutated: "${ key } "` ,
40
+ vm
41
+ )
42
+ } )
43
+ } else {
44
+ defineReactive ( vm , key , source . _provided [ provideKey ] )
45
+ }
34
46
break
35
47
}
36
48
source = source . $parent
Original file line number Diff line number Diff line change @@ -196,4 +196,25 @@ describe('Options provide/inject', () => {
196
196
} )
197
197
} )
198
198
} )
199
+
200
+ it ( 'should warn when injections has been modified' , ( ) => {
201
+ const key = 'foo'
202
+ const vm = new Vue ( {
203
+ provide : {
204
+ foo : 1
205
+ }
206
+ } )
207
+
208
+ const child = new Vue ( {
209
+ parent : vm ,
210
+ inject : [ 'foo' ]
211
+ } )
212
+
213
+ expect ( child . foo ) . toBe ( 1 )
214
+ child . foo = 2
215
+ expect (
216
+ `Avoid mutating a injections directly since the value will be ` +
217
+ `overwritten whenever the provided component re-renders. ` +
218
+ `injections being mutated: "${ key } "` ) . toHaveBeenWarned ( )
219
+ } )
199
220
} )
You can’t perform that action at this time.
0 commit comments