From d70c90495b3082625171fe1ef3d9db4705ed09e5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 24 Jun 2020 18:24:18 +0800 Subject: [PATCH] feat: export nextTick (#401) * feat: export nextTick * chore: update --- src/index.ts | 1 + src/nextTick.ts | 11 +++++++++++ test/misc.spec.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 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..4a912657 --- /dev/null +++ b/src/nextTick.ts @@ -0,0 +1,11 @@ +import Vue from 'vue' +import { currentVue } from './runtimeContext' + +type NextTick = Vue['$nextTick'] + +export const nextTick: NextTick = function nextTick( + this: ThisType, + ...args: Parameters +) { + return currentVue?.nextTick.bind(this, 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') + }) + }) + }) +})