Skip to content

Commit 33e669b

Browse files
sqalyyx990803
authored andcommitted
fix(ssr): computed properties should pass vm as first argument in ssr (#9090)
fix #8977
1 parent f43ae6d commit 33e669b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/core/instance/state.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ export function defineComputed (
216216
if (typeof userDef === 'function') {
217217
sharedPropertyDefinition.get = shouldCache
218218
? createComputedGetter(key)
219-
: userDef
219+
: createGetterInvoker(userDef)
220220
sharedPropertyDefinition.set = noop
221221
} else {
222222
sharedPropertyDefinition.get = userDef.get
223223
? shouldCache && userDef.cache !== false
224224
? createComputedGetter(key)
225-
: userDef.get
225+
: createGetterInvoker(userDef.get)
226226
: noop
227227
sharedPropertyDefinition.set = userDef.set || noop
228228
}
@@ -253,6 +253,12 @@ function createComputedGetter (key) {
253253
}
254254
}
255255

256+
function createGetterInvoker(fn) {
257+
return function computedGetter () {
258+
return fn.call(this, this)
259+
}
260+
}
261+
256262
function initMethods (vm: Component, methods: Object) {
257263
const props = vm.$options.props
258264
for (const key in methods) {

test/ssr/ssr-string.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,24 @@ describe('SSR: renderToString', () => {
10761076
})
10771077
})
10781078

1079+
// #8977
1080+
it('should call computed properties with vm as first argument', done => {
1081+
renderToString(new Vue({
1082+
data: {
1083+
firstName: 'Evan',
1084+
lastName: 'You'
1085+
},
1086+
computed: {
1087+
fullName: ({ firstName, lastName }) => `${firstName} ${lastName}`,
1088+
},
1089+
template: '<div>{{ fullName }}</div>',
1090+
}), (err, result) => {
1091+
expect(err).toBeNull()
1092+
expect(result).toContain('<div data-server-rendered="true">Evan You</div>')
1093+
done()
1094+
})
1095+
})
1096+
10791097
it('return Promise', done => {
10801098
renderToString(new Vue({
10811099
template: `<div>{{ foo }}</div>`,

0 commit comments

Comments
 (0)