Skip to content

Commit

Permalink
fix(ssr): computed properties should pass vm as first argument in ssr (
Browse files Browse the repository at this point in the history
  • Loading branch information
sqal authored and yyx990803 committed Dec 1, 2018
1 parent f43ae6d commit 33e669b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ export function defineComputed (
if (typeof userDef === 'function') {
sharedPropertyDefinition.get = shouldCache
? createComputedGetter(key)
: userDef
: createGetterInvoker(userDef)
sharedPropertyDefinition.set = noop
} else {
sharedPropertyDefinition.get = userDef.get
? shouldCache && userDef.cache !== false
? createComputedGetter(key)
: userDef.get
: createGetterInvoker(userDef.get)
: noop
sharedPropertyDefinition.set = userDef.set || noop
}
Expand Down Expand Up @@ -253,6 +253,12 @@ function createComputedGetter (key) {
}
}

function createGetterInvoker(fn) {
return function computedGetter () {
return fn.call(this, this)
}
}

function initMethods (vm: Component, methods: Object) {
const props = vm.$options.props
for (const key in methods) {
Expand Down
18 changes: 18 additions & 0 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,24 @@ describe('SSR: renderToString', () => {
})
})

// #8977
it('should call computed properties with vm as first argument', done => {
renderToString(new Vue({
data: {
firstName: 'Evan',
lastName: 'You'
},
computed: {
fullName: ({ firstName, lastName }) => `${firstName} ${lastName}`,
},
template: '<div>{{ fullName }}</div>',
}), (err, result) => {
expect(err).toBeNull()
expect(result).toContain('<div data-server-rendered="true">Evan You</div>')
done()
})
})

it('return Promise', done => {
renderToString(new Vue({
template: `<div>{{ foo }}</div>`,
Expand Down

0 comments on commit 33e669b

Please sign in to comment.