From 391a102931a6faf7b0c9fe1f61accfc5e48fe8d9 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 22 Jun 2020 22:02:55 +0800 Subject: [PATCH 1/2] feat: export nextTick --- src/index.ts | 1 + src/nextTick.ts | 13 +++++++++++++ test/misc.spec.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/nextTick.ts create mode 100644 test/misc.spec.js diff --git a/src/index.ts b/src/index.ts index c891f3d1..4d149bf1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,7 @@ if (currentVue && typeof window !== 'undefined' && window.Vue) { } export default plugin +export { nextTick } from './nextTick' export { default as createElement } from './createElement' export { SetupContext } export { diff --git a/src/nextTick.ts b/src/nextTick.ts new file mode 100644 index 00000000..063abd71 --- /dev/null +++ b/src/nextTick.ts @@ -0,0 +1,13 @@ +import Vue from 'vue' +import { currentVM, currentVue } from './runtimeContext' + +type NextTick = Vue['$nextTick'] + +export const nextTick: NextTick = function nextTick(...args: any) { + if (currentVM) { + return currentVM.$nextTick.apply(currentVM, args) + } else { + // @ts-ignore + return currentVue?.nextTick(...args) + } +} as any diff --git a/test/misc.spec.js b/test/misc.spec.js new file mode 100644 index 00000000..6c10bb66 --- /dev/null +++ b/test/misc.spec.js @@ -0,0 +1,29 @@ +const Vue = require('vue/dist/vue.common.js') +const { ref, nextTick } = require('../src') + +describe('nextTick', () => { + it('should works', () => { + const vm = new Vue({ + template: `
{{a}}
`, + setup() { + return { + a: ref(1), + } + }, + }).$mount() + + expect(vm.$el.textContent).toBe('1') + vm.a = 2 + expect(vm.$el.textContent).toBe('1') + + nextTick(() => { + expect(vm.$el.textContent).toBe('2') + vm.a = 3 + expect(vm.$el.textContent).toBe('2') + + nextTick(() => { + expect(vm.$el.textContent).toBe('3') + }) + }) + }) +}) From f54fa48dfe25640f0a188bcdb8871e3da9e4270b Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 23 Jun 2020 12:18:18 +0800 Subject: [PATCH 2/2] chore: update --- src/nextTick.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/nextTick.ts b/src/nextTick.ts index 063abd71..4a912657 100644 --- a/src/nextTick.ts +++ b/src/nextTick.ts @@ -1,13 +1,11 @@ import Vue from 'vue' -import { currentVM, currentVue } from './runtimeContext' +import { currentVue } from './runtimeContext' type NextTick = Vue['$nextTick'] -export const nextTick: NextTick = function nextTick(...args: any) { - if (currentVM) { - return currentVM.$nextTick.apply(currentVM, args) - } else { - // @ts-ignore - return currentVue?.nextTick(...args) - } +export const nextTick: NextTick = function nextTick( + this: ThisType, + ...args: Parameters +) { + return currentVue?.nextTick.bind(this, args) } as any