Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I inject posts into a Vue component (not layout)? #595

Open
ninest opened this issue Mar 12, 2020 · 3 comments
Open

Can I inject posts into a Vue component (not layout)? #595

ninest opened this issue Mar 12, 2020 · 3 comments

Comments

@ninest
Copy link

ninest commented Mar 12, 2020

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?

@krmax44
Copy link
Contributor

krmax44 commented Mar 12, 2020

You'd have to pass the page prop manually, like this:

// in your layout file:
<ComponentToc :page="page" />

@ninest
Copy link
Author

ninest commented Mar 12, 2020

@krmax44 Thanks for the quick reply. Is there a chance that the ability to inject posts into components will be added soon?

@krmax44
Copy link
Contributor

krmax44 commented Mar 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants