Skip to content

Commit

Permalink
fix(components): guess vue3 anonymous components name, fixes #1311
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed May 24, 2021
1 parent 21337bc commit 097d629
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
18 changes: 14 additions & 4 deletions packages/app-backend-vue3/src/components/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ export function isFragment (instance) {
export function getInstanceName (instance) {
const name = getComponentTypeName(instance.type || {})
if (name) return name
return instance.root === instance
? 'Root'
: 'Anonymous Component'
if (instance.root === instance) return 'Root'
for (const key in instance.parent?.type?.components) {
if (instance.parent.type.components[key] === instance.type) return saveComponentName(instance, key)
}
for (const key in instance.appContext?.components) {
if (instance.appContext.components[key] === instance.type) return saveComponentName(instance, key)
}
return 'Anonymous Component'
}

function saveComponentName (instance, key) {
instance.type.__vdevtools_guessedName = key
return key
}

function getComponentTypeName (options) {
const name = options.name || options._componentTag
const name = options.name || options._componentTag || options.__vdevtools_guessedName
if (name) {
return name
}
Expand Down
13 changes: 12 additions & 1 deletion packages/shell-dev-vue2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ for (let i = 0; i < 100; i++) {
const circular = {}
circular.self = circular

Vue.component('global', {
render: h => h('h3', 'Global component')
})

const app = new Vue({
store,
router,
components: {
inline: {
render: h => h('h3', 'Inline component definition')
}
},
data: {
obj: {
items: items,
Expand All @@ -47,7 +56,9 @@ const app = new Vue({
h(VuexObject),
h(Init),
h(RefTester),
h(Hidden)
h(Hidden),
h('global'),
h('inline')
])
}
})
Expand Down
9 changes: 8 additions & 1 deletion packages/shell-dev-vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import SetupRender from './SetupRender.js'
import Form from './Form.vue'
import Heavy from './Heavy.vue'
import { h } from 'vue'
export default {
name: 'MyApp',
Expand All @@ -33,7 +35,10 @@ export default {
Other,
SetupRender,
Form,
Heavy
Heavy,
inline: {
render: () => h('h3', 'Inline component definition')
}
},
data () {
Expand Down Expand Up @@ -77,6 +82,8 @@ export default {
<Other />
<SetupRender />
<Form />
<inline />
<global />

<nav>
<router-link to="/p1">
Expand Down
3 changes: 3 additions & 0 deletions packages/shell-dev-vue3/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const router = createRouter({
})

const app = createApp(App)
app.component('global', {
render: () => 'I\'m a global component'
})
app.use(TestPlugin)
app.use(router)
app.mount('#app')
Expand Down

0 comments on commit 097d629

Please sign in to comment.