Przeglądaj źródła

fix: support plugin with multi version vue (#5985)

close #5970
wenlu.wang 8 lat temu
rodzic
commit
049f3171a9

+ 4 - 6
src/core/global-api/use.js

@@ -4,13 +4,11 @@ import { toArray } from '../util/index'
 
 export function initUse (Vue: GlobalAPI) {
   Vue.use = function (plugin: Function | Object) {
-    const cid = this.cid
-    if (!plugin._installed) {
-      plugin._installed = {}
-    }
-    if (plugin._installed[cid]) {
+    const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
+    if (installedPlugins.indexOf(plugin) > -1) {
       return this
     }
+
     // additional parameters
     const args = toArray(arguments, 1)
     args.unshift(this)
@@ -19,7 +17,7 @@ export function initUse (Vue: GlobalAPI) {
     } else if (typeof plugin === 'function') {
       plugin.apply(null, args)
     }
-    plugin._installed[cid] = true
+    installedPlugins.push(plugin)
     return this
   }
 }

+ 16 - 0
test/unit/features/global-api/use.spec.js

@@ -33,4 +33,20 @@ describe('Global API: use', () => {
     expect(Vue.options.directives['plugin-test']).toBeUndefined()
     expect(Ctor.options.directives['plugin-test']).toBe(def)
   })
+
+  // Github issue #5970
+  it('should work on multi version', () => {
+    const Ctor1 = Vue.extend({})
+    const Ctor2 = Vue.extend({})
+
+    Ctor1.use(pluginStub, options)
+    expect(Vue.options.directives['plugin-test']).toBeUndefined()
+    expect(Ctor1.options.directives['plugin-test']).toBe(def)
+
+    // multi version Vue Ctor with the same cid
+    Ctor2.cid = Ctor1.cid
+    Ctor2.use(pluginStub, options)
+    expect(Vue.options.directives['plugin-test']).toBeUndefined()
+    expect(Ctor2.options.directives['plugin-test']).toBe(def)
+  })
 })