Преглед на файлове

fix global directive function shorthand (fix #3243)

Evan You преди 10 години
родител
ревизия
42889ff87a
променени са 2 файла, в които са добавени 24 реда и са изтрити 0 реда
  1. 3 0
      src/core/global-api/assets.js
  2. 21 0
      test/unit/features/options/directives.spec.js

+ 3 - 0
src/core/global-api/assets.js

@@ -28,6 +28,9 @@ export function initAssetRegisters (Vue: GlobalAPI) {
           definition.name = definition.name || id
           definition = Vue.extend(definition)
         }
+        if (type === 'directive' && typeof definition === 'function') {
+          definition = { bind: definition, update: definition }
+        }
         this.options[type + 's'][id] = definition
         return definition
       }

+ 21 - 0
test/unit/features/options/directives.spec.js

@@ -92,6 +92,27 @@ describe('Options directives', () => {
     }).then(done)
   })
 
+  it('function shorthand (global)', done => {
+    const spy = jasmine.createSpy('directive')
+    Vue.directive('test', function (el, binding, vnode) {
+      expect(vnode.context).toBe(vm)
+      expect(binding.arg).toBe('arg')
+      expect(binding.modifiers).toEqual({ hello: true })
+      spy(binding.value, binding.oldValue)
+    })
+    const vm = new Vue({
+      template: '<div v-test:arg.hello="a"></div>',
+      data: { a: 'foo' }
+    })
+    vm.$mount()
+    expect(spy).toHaveBeenCalledWith('foo', undefined)
+    vm.a = 'bar'
+    waitForUpdate(() => {
+      expect(spy).toHaveBeenCalledWith('bar', 'foo')
+      delete Vue.options.directives.test
+    }).then(done)
+  })
+
   it('warn non-existent', () => {
     new Vue({
       template: '<div v-test></div>'