Skip to content

Commit 6034c3e

Browse files
committed
ensure methods are available in data() (close vuejs#4302)
1 parent c32ea05 commit 6034c3e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/core/instance/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import {
2525
export function initState (vm: Component) {
2626
vm._watchers = []
2727
initProps(vm)
28+
initMethods(vm)
2829
initData(vm)
2930
initComputed(vm)
30-
initMethods(vm)
3131
initWatch(vm)
3232
}
3333

test/unit/features/options/data.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,34 @@ describe('Options data', () => {
7575
vm.$data = {}
7676
expect('Avoid replacing instance root $data').toHaveBeenWarned()
7777
})
78+
79+
it('should have access to props', () => {
80+
const Test = {
81+
props: ['a'],
82+
data () {
83+
return {
84+
b: this.a
85+
}
86+
}
87+
}
88+
const vm = new Vue({
89+
template: `<test ref="test" :a="1"></test>`,
90+
components: { Test }
91+
}).$mount()
92+
expect(vm.$refs.test.b).toBe(1)
93+
})
94+
95+
it('should have access to methods', () => {
96+
const vm = new Vue({
97+
methods: {
98+
get () {
99+
return { a: 1 }
100+
}
101+
},
102+
data () {
103+
return this.get()
104+
}
105+
})
106+
expect(vm.a).toBe(1)
107+
})
78108
})

0 commit comments

Comments
 (0)