Przeglądaj źródła

ensure local assets is prioritized regardless of naming convention (fix #4434)

Evan You 9 lat temu
rodzic
commit
2afa2601e0

+ 8 - 5
src/core/util/options.js

@@ -319,11 +319,14 @@ export function resolveAsset (
     return
   }
   const assets = options[type]
-  const res = assets[id] ||
-    // camelCase ID
-    assets[camelize(id)] ||
-    // Pascal Case ID
-    assets[capitalize(camelize(id))]
+  // check local registration variations first
+  if (hasOwn(assets, id)) return assets[id]
+  const camelizedId = camelize(id)
+  if (hasOwn(assets, camelizedId)) return assets[camelizedId]
+  const PascalCaseId = capitalize(camelizedId)
+  if (hasOwn(assets, PascalCaseId)) return assets[PascalCaseId]
+  // fallback to prototype chain
+  const res = assets[id] || assets[camelizedId] || assets[PascalCaseId]
   if (process.env.NODE_ENV !== 'production' && warnMissing && !res) {
     warn(
       'Failed to resolve ' + type.slice(0, -1) + ': ' + id,

+ 17 - 0
test/unit/features/global-api/assets.spec.js

@@ -48,4 +48,21 @@ describe('Global API: assets', () => {
     // extended registration should not pollute global
     expect(Vue.options.components.test).toBeUndefined()
   })
+
+  // #4434
+  it('local registration should take priority regardless of naming convention', () => {
+    Vue.component('x-foo', {
+      template: '<span>global</span>'
+    })
+    const vm = new Vue({
+      components: {
+        xFoo: {
+          template: '<span>local</span>'
+        }
+      },
+      template: '<div><x-foo></x-foo></div>'
+    }).$mount()
+    expect(vm.$el.textContent).toBe('local')
+    delete Vue.options.components['x-foo']
+  })
 })