fix(runtime-core): component methods should override global properties in DEV (#3074)
This commit is contained in:
parent
450f8883c3
commit
2587f36fe3
@ -8,7 +8,8 @@ import {
|
|||||||
nextTick,
|
nextTick,
|
||||||
renderToString,
|
renderToString,
|
||||||
ref,
|
ref,
|
||||||
defineComponent
|
defineComponent,
|
||||||
|
createApp
|
||||||
} from '@vue/runtime-test'
|
} from '@vue/runtime-test'
|
||||||
|
|
||||||
describe('api: options', () => {
|
describe('api: options', () => {
|
||||||
@ -105,6 +106,24 @@ describe('api: options', () => {
|
|||||||
expect(serializeInner(root)).toBe(`<div>2</div>`)
|
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 () => {
|
test('watch', async () => {
|
||||||
function returnThis(this: any) {
|
function returnThis(this: any) {
|
||||||
return this
|
return this
|
||||||
|
@ -604,7 +604,17 @@ export function applyOptions(
|
|||||||
for (const key in methods) {
|
for (const key in methods) {
|
||||||
const methodHandler = (methods as MethodOptions)[key]
|
const methodHandler = (methods as MethodOptions)[key]
|
||||||
if (isFunction(methodHandler)) {
|
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__) {
|
if (__DEV__) {
|
||||||
checkDuplicateProperties!(OptionTypes.METHODS, key)
|
checkDuplicateProperties!(OptionTypes.METHODS, key)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user