Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): computed properties first argument #9090

Merged
merged 1 commit into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
? userDef.set
Expand Down Expand Up @@ -255,6 +255,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