]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): ensure this context for $nextTick callback
authorEvan You <yyx990803@gmail.com>
Mon, 5 Oct 2020 22:18:38 +0000 (18:18 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 5 Oct 2020 22:18:38 +0000 (18:18 -0400)
fix #2282

packages/runtime-core/__tests__/componentProxy.spec.ts
packages/runtime-core/src/componentPublicInstance.ts
packages/runtime-core/src/scheduler.ts

index f37bfe3d9d86f60af54e3063ccb05e0d935a4d19..4a4b56a8473a5ca3307ef610001194db4c3f091b 100644 (file)
@@ -69,7 +69,7 @@ describe('component: proxy', () => {
     expect('count' in instanceProxy).toBe(false)
   })
 
-  test('public properties', () => {
+  test('public properties', async () => {
     let instance: ComponentInternalInstance
     let instanceProxy: any
     const Comp = {
@@ -96,6 +96,11 @@ describe('component: proxy', () => {
     expect(instanceProxy.$options).toBe(instance!.type)
     expect(() => (instanceProxy.$data = {})).toThrow(TypeError)
     expect(`Attempting to mutate public property "$data"`).toHaveBeenWarned()
+
+    const nextTickThis = await instanceProxy.$nextTick(function(this: any) {
+      return this
+    })
+    expect(nextTickThis).toBe(instanceProxy)
   })
 
   test('user attached properties', async () => {
index e0e239a7997dcb538d89fe5b6d9b5e65fe84666b..e45fd7755f4eeaaf01d726f84a95840ea425e6f7 100644 (file)
@@ -213,7 +213,7 @@ const publicPropertiesMap: PublicPropertiesMap = extend(Object.create(null), {
   $emit: i => i.emit,
   $options: i => (__FEATURE_OPTIONS_API__ ? resolveMergedOptions(i) : i.type),
   $forceUpdate: i => () => queueJob(i.update),
-  $nextTick: () => nextTick,
+  $nextTick: i => nextTick.bind(i.proxy!),
   $watch: i => (__FEATURE_OPTIONS_API__ ? instanceWatch.bind(i) : NOOP)
 } as PublicPropertiesMap)
 
index 9c90f5c53b5db4f31c6ca2512c255e0765ae93f8..32fce93cc85b3556fc3a836591516a613fdaefb1 100644 (file)
@@ -1,5 +1,6 @@
 import { ErrorCodes, callWithErrorHandling } from './errorHandling'
 import { isArray } from '@vue/shared'
+import { ComponentPublicInstance } from './componentPublicInstance'
 
 export interface SchedulerJob {
   (): void
@@ -48,9 +49,12 @@ let currentPreFlushParentJob: SchedulerJob | null = null
 const RECURSION_LIMIT = 100
 type CountMap = Map<SchedulerJob | SchedulerCb, number>
 
-export function nextTick(fn?: () => void): Promise<void> {
+export function nextTick(
+  this: ComponentPublicInstance | void,
+  fn?: () => void
+): Promise<void> {
   const p = currentFlushPromise || resolvedPromise
-  return fn ? p.then(fn) : p
+  return fn ? p.then(this ? fn.bind(this) : fn) : p
 }
 
 export function queueJob(job: SchedulerJob) {