Skip to content

Commit

Permalink
fix(runtime-core): component methods should override global propertie…
Browse files Browse the repository at this point in the history
…s in DEV (#3074)
  • Loading branch information
HcySunYang authored Feb 5, 2021
1 parent 450f888 commit 2587f36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
nextTick,
renderToString,
ref,
defineComponent
defineComponent,
createApp
} from '@vue/runtime-test'

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

test('component’s own methods have higher priority than global properties', async () => {
const app = createApp({
methods: {
foo() {
return 'foo'
}
},
render() {
return this.foo()
}
})
app.config.globalProperties.foo = () => 'bar'

const root = nodeOps.createElement('div')
app.mount(root)
expect(serializeInner(root)).toBe(`foo`)
})

test('watch', async () => {
function returnThis(this: any) {
return this
Expand Down
12 changes: 11 additions & 1 deletion packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,17 @@ export function applyOptions(
for (const key in methods) {
const methodHandler = (methods as MethodOptions)[key]
if (isFunction(methodHandler)) {
ctx[key] = methodHandler.bind(publicThis)
// In dev mode, we use the `createRenderContext` function to define methods to the proxy target,
// and those are read-only but reconfigurable, so it needs to be redefined here
if (__DEV__) {
Object.defineProperty(ctx, key, {
value: methodHandler.bind(publicThis),
configurable: true,
enumerable: false
})
} else {
ctx[key] = methodHandler.bind(publicThis)
}
if (__DEV__) {
checkDuplicateProperties!(OptionTypes.METHODS, key)
}
Expand Down

0 comments on commit 2587f36

Please sign in to comment.