Update component injection example

This commit is contained in:
Sébastien Chopin 2017-07-10 08:56:02 +02:00
parent 2cc78c0446
commit 3e36219a43

View File

@ -1,16 +1,19 @@
<template>
<BlogArticle/>
<component :is="comp"/>
</template>
<script>
const components = {}
const files = require.context('@/articles', false, /\.vue$/)
files.keys().forEach((filename) => {
const name = filename.replace('./', '').replace('.vue', '')
components[name] = () => import('@/articles/' + name + '.vue').then((m) => m.default || m)
})
export default {
async asyncData({ params, error }) {
const slug = params.slug || 'hello'
try {
this.components.BlogArticle = await import(`~/articles/${slug}.vue`)
} catch (e) {
error({ statusCode: 404, message: 'Article not found' })
}
}
return { comp: params.slug }
},
components
}
</script>