Skip to content

Commit 42889ff

Browse files
committed
fix global directive function shorthand (fix vuejs#3243)
1 parent dffeb1d commit 42889ff

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/core/global-api/assets.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export function initAssetRegisters (Vue: GlobalAPI) {
2828
definition.name = definition.name || id
2929
definition = Vue.extend(definition)
3030
}
31+
if (type === 'directive' && typeof definition === 'function') {
32+
definition = { bind: definition, update: definition }
33+
}
3134
this.options[type + 's'][id] = definition
3235
return definition
3336
}

test/unit/features/options/directives.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ describe('Options directives', () => {
9292
}).then(done)
9393
})
9494

95+
it('function shorthand (global)', done => {
96+
const spy = jasmine.createSpy('directive')
97+
Vue.directive('test', function (el, binding, vnode) {
98+
expect(vnode.context).toBe(vm)
99+
expect(binding.arg).toBe('arg')
100+
expect(binding.modifiers).toEqual({ hello: true })
101+
spy(binding.value, binding.oldValue)
102+
})
103+
const vm = new Vue({
104+
template: '<div v-test:arg.hello="a"></div>',
105+
data: { a: 'foo' }
106+
})
107+
vm.$mount()
108+
expect(spy).toHaveBeenCalledWith('foo', undefined)
109+
vm.a = 'bar'
110+
waitForUpdate(() => {
111+
expect(spy).toHaveBeenCalledWith('bar', 'foo')
112+
delete Vue.options.directives.test
113+
}).then(done)
114+
})
115+
95116
it('warn non-existent', () => {
96117
new Vue({
97118
template: '<div v-test></div>'

0 commit comments

Comments
 (0)