Skip to content

Commit 391a102

Browse files
committed
feat: export nextTick
1 parent 58d8d9a commit 391a102

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if (currentVue && typeof window !== 'undefined' && window.Vue) {
2121
}
2222

2323
export default plugin
24+
export { nextTick } from './nextTick'
2425
export { default as createElement } from './createElement'
2526
export { SetupContext }
2627
export {

src/nextTick.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Vue from 'vue'
2+
import { currentVM, currentVue } from './runtimeContext'
3+
4+
type NextTick = Vue['$nextTick']
5+
6+
export const nextTick: NextTick = function nextTick(...args: any) {
7+
if (currentVM) {
8+
return currentVM.$nextTick.apply(currentVM, args)
9+
} else {
10+
// @ts-ignore
11+
return currentVue?.nextTick(...args)
12+
}
13+
} as any

test/misc.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Vue = require('vue/dist/vue.common.js')
2+
const { ref, nextTick } = require('../src')
3+
4+
describe('nextTick', () => {
5+
it('should works', () => {
6+
const vm = new Vue({
7+
template: `<div>{{a}}</div>`,
8+
setup() {
9+
return {
10+
a: ref(1),
11+
}
12+
},
13+
}).$mount()
14+
15+
expect(vm.$el.textContent).toBe('1')
16+
vm.a = 2
17+
expect(vm.$el.textContent).toBe('1')
18+
19+
nextTick(() => {
20+
expect(vm.$el.textContent).toBe('2')
21+
vm.a = 3
22+
expect(vm.$el.textContent).toBe('2')
23+
24+
nextTick(() => {
25+
expect(vm.$el.textContent).toBe('3')
26+
})
27+
})
28+
})
29+
})

0 commit comments

Comments
 (0)