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') + }) + }) + }) +})