Skip to content

Commit 0f0c260

Browse files
committed
fix: override instance watchers with mounting options watchers
1 parent 9f40996 commit 0f0c260

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

packages/create-instance/create-instance.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ export default function createInstance(
7373

7474
// make sure all extends are based on this instance
7575

76+
if (instanceOptions.watch) {
77+
console.log(instanceOptions.watch)
78+
}
79+
80+
// watchers provided in mounting options should override preexisting ones
81+
if (componentOptions.watch && instanceOptions.watch) {
82+
const componentWatchers = Object.keys(componentOptions.watch)
83+
const instanceWatchers = Object.keys(instanceOptions.watch)
84+
85+
for (let i = 0; i < instanceWatchers.length; i++) {
86+
const k = instanceWatchers[i]
87+
// override the componentOptions with the one provided in mounting options
88+
if (componentWatchers.includes(k)) {
89+
componentOptions.watch[k] = instanceOptions.watch[k]
90+
}
91+
}
92+
}
93+
7694
const Constructor = _Vue.extend(componentOptions).extend(instanceOptions)
7795
componentOptions._Ctor = {}
7896
Constructor.options._base = _Vue

test/specs/mounting-options/watch.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import {
2-
describeWithShallowAndMount,
3-
isRunningPhantomJS,
4-
vueVersion
5-
} from '~resources/utils'
1+
import { describeWithShallowAndMount } from '~resources/utils'
62
import { itSkipIf, itDoNotRunIf } from 'conditional-specs'
73

84
describeWithShallowAndMount('options.watch', mountingMethod => {
95
it('overrides a default watch handler', async () => {
106
const TestComponent = {
11-
props: ['someProp'],
7+
props: ['someProp', 'anotherProp'],
128
template: '<div>{{ foo }}</div>',
139
data() {
1410
return {
1511
foo: 'bar'
1612
}
1713
},
1814
watch: {
15+
anotherProp: {
16+
handler() {
17+
// placeholder
18+
}
19+
},
1920
someProp: {
2021
handler() {
2122
this.foo = 'updated-bar'

0 commit comments

Comments
 (0)