Skip to content

Commit 2587f36

Browse files
authored
fix(runtime-core): component methods should override global properties in DEV (#3074)
1 parent 450f888 commit 2587f36

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/runtime-core/__tests__/apiOptions.spec.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
nextTick,
99
renderToString,
1010
ref,
11-
defineComponent
11+
defineComponent,
12+
createApp
1213
} from '@vue/runtime-test'
1314

1415
describe('api: options', () => {
@@ -105,6 +106,24 @@ describe('api: options', () => {
105106
expect(serializeInner(root)).toBe(`<div>2</div>`)
106107
})
107108

109+
test('component’s own methods have higher priority than global properties', async () => {
110+
const app = createApp({
111+
methods: {
112+
foo() {
113+
return 'foo'
114+
}
115+
},
116+
render() {
117+
return this.foo()
118+
}
119+
})
120+
app.config.globalProperties.foo = () => 'bar'
121+
122+
const root = nodeOps.createElement('div')
123+
app.mount(root)
124+
expect(serializeInner(root)).toBe(`foo`)
125+
})
126+
108127
test('watch', async () => {
109128
function returnThis(this: any) {
110129
return this

packages/runtime-core/src/componentOptions.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,17 @@ export function applyOptions(
604604
for (const key in methods) {
605605
const methodHandler = (methods as MethodOptions)[key]
606606
if (isFunction(methodHandler)) {
607-
ctx[key] = methodHandler.bind(publicThis)
607+
// In dev mode, we use the `createRenderContext` function to define methods to the proxy target,
608+
// and those are read-only but reconfigurable, so it needs to be redefined here
609+
if (__DEV__) {
610+
Object.defineProperty(ctx, key, {
611+
value: methodHandler.bind(publicThis),
612+
configurable: true,
613+
enumerable: false
614+
})
615+
} else {
616+
ctx[key] = methodHandler.bind(publicThis)
617+
}
608618
if (__DEV__) {
609619
checkDuplicateProperties!(OptionTypes.METHODS, key)
610620
}

0 commit comments

Comments
 (0)