Skip to content

Commit 097d629

Browse files
committed
fix(components): guess vue3 anonymous components name, fixes #1311
1 parent 21337bc commit 097d629

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

packages/app-backend-vue3/src/components/util.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ export function isFragment (instance) {
2929
export function getInstanceName (instance) {
3030
const name = getComponentTypeName(instance.type || {})
3131
if (name) return name
32-
return instance.root === instance
33-
? 'Root'
34-
: 'Anonymous Component'
32+
if (instance.root === instance) return 'Root'
33+
for (const key in instance.parent?.type?.components) {
34+
if (instance.parent.type.components[key] === instance.type) return saveComponentName(instance, key)
35+
}
36+
for (const key in instance.appContext?.components) {
37+
if (instance.appContext.components[key] === instance.type) return saveComponentName(instance, key)
38+
}
39+
return 'Anonymous Component'
40+
}
41+
42+
function saveComponentName (instance, key) {
43+
instance.type.__vdevtools_guessedName = key
44+
return key
3545
}
3646

3747
function getComponentTypeName (options) {
38-
const name = options.name || options._componentTag
48+
const name = options.name || options._componentTag || options.__vdevtools_guessedName
3949
if (name) {
4050
return name
4151
}

packages/shell-dev-vue2/src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ for (let i = 0; i < 100; i++) {
2626
const circular = {}
2727
circular.self = circular
2828

29+
Vue.component('global', {
30+
render: h => h('h3', 'Global component')
31+
})
32+
2933
const app = new Vue({
3034
store,
3135
router,
36+
components: {
37+
inline: {
38+
render: h => h('h3', 'Inline component definition')
39+
}
40+
},
3241
data: {
3342
obj: {
3443
items: items,
@@ -47,7 +56,9 @@ const app = new Vue({
4756
h(VuexObject),
4857
h(Init),
4958
h(RefTester),
50-
h(Hidden)
59+
h(Hidden),
60+
h('global'),
61+
h('inline')
5162
])
5263
}
5364
})

packages/shell-dev-vue3/src/App.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import SetupRender from './SetupRender.js'
1515
import Form from './Form.vue'
1616
import Heavy from './Heavy.vue'
1717
18+
import { h } from 'vue'
19+
1820
export default {
1921
name: 'MyApp',
2022
@@ -33,7 +35,10 @@ export default {
3335
Other,
3436
SetupRender,
3537
Form,
36-
Heavy
38+
Heavy,
39+
inline: {
40+
render: () => h('h3', 'Inline component definition')
41+
}
3742
},
3843
3944
data () {
@@ -77,6 +82,8 @@ export default {
7782
<Other />
7883
<SetupRender />
7984
<Form />
85+
<inline />
86+
<global />
8087

8188
<nav>
8289
<router-link to="/p1">

packages/shell-dev-vue3/src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const router = createRouter({
2323
})
2424

2525
const app = createApp(App)
26+
app.component('global', {
27+
render: () => 'I\'m a global component'
28+
})
2629
app.use(TestPlugin)
2730
app.use(router)
2831
app.mount('#app')

0 commit comments

Comments
 (0)