Can I inject posts into a Vue component (not layout)?
I have a table of contents component which shows a list of posts. I tried this:
// components/toc.vue
<script>
export const data = {
injectAllPosts: true
}
export default {
props: ["page", ...],
...
When I check the Vue devtools, page does not have any property posts. Is there a way to do this?
You'd have to pass the page prop manually, like this:
// in your layout file:
<ComponentToc :page="page" />
@krmax44 Thanks for the quick reply. Is there a chance that the ability to inject posts into components will be added soon?
I don't really see a wide use-case for that. Components aren't meant to have page state, but to be page/layout-agnostic. A component usually is something like a button. If you want to create a post component used on the index page, category/tag pages etc. it is meant to be used like this:
<BlogPost :title="post.title" :createdAt="post.createdAt" />
<!--- or like this -->
<BlogPost :post="post" />
For page state, layouts are the intended way.