From 33e669b22f69a1f9c9147528360fe0bba85534f0 Mon Sep 17 00:00:00 2001 From: sqal Date: Sat, 1 Dec 2018 06:05:13 +0100 Subject: [PATCH] fix(ssr): computed properties should pass vm as first argument in ssr (#9090) fix #8977 --- src/core/instance/state.js | 10 ++++++++-- test/ssr/ssr-string.spec.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 9fec53f6..cc1401a3 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -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 } @@ -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) { diff --git a/test/ssr/ssr-string.spec.js b/test/ssr/ssr-string.spec.js index abf91526..a84c7267 100644 --- a/test/ssr/ssr-string.spec.js +++ b/test/ssr/ssr-string.spec.js @@ -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: '
{{ fullName }}
', + }), (err, result) => { + expect(err).toBeNull() + expect(result).toContain('
Evan You
') + done() + }) + }) + it('return Promise', done => { renderToString(new Vue({ template: `
{{ foo }}
`,