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

fix: useMeta() throws error if used in a layout #64

Closed
miii opened this issue May 18, 2020 · 3 comments
Closed

fix: useMeta() throws error if used in a layout #64

miii opened this issue May 18, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@miii
Copy link
Contributor

miii commented May 18, 2020

🐛 The bug
The head: {} property triggers an error inside @vue/composition-api if used in a layout component, thus useMeta() cannot be used.

[vue-composition-api] must call Vue.use(plugin) before using any function

Pages seems to work as expected. The problem is caused by reactive() not being used in the correct scope (too soon in the Nuxt lifecycle?).

export const getHeadOptions = (options: any) => {
const _head: ReactiveHead = reactive<MetaInfo>({})
if (!(options.head instanceof Function)) {
Object.assign(_head, options.head)
}
const head =
options.head instanceof Function
? () => defu(_head, options.head())
: () => _head
return { _head, head }
}

I've resolved the issue myself in a fork by using Vue.observable() instead of reactive().
I'll submit a PR when it's ready to be reviewed.

🛠️ To reproduce
Steps to reproduce the behavior:

  1. Add head: {} to your layout
  2. See error

Use this sandbox to reproduce the error.
https://codesandbox.io/s/nuxt-composition-api-usemeta-layout-issue-r14yy?file=/layouts/default.vue

🌈 Expected behaviour
useMeta() should work in layout components as expected.

@miii miii added the bug Something isn't working label May 18, 2020
miii added a commit to miii/composition-api that referenced this issue May 18, 2020
Set and update head metadata anytime in your setup method

✅ Closes: nuxt-community#64
miii added a commit to miii/composition-api that referenced this issue May 18, 2020
Set or update head metadata anytime in your setup method

✅ Closes: nuxt-community#64
miii added a commit to miii/composition-api that referenced this issue May 18, 2020
Set or update head metadata anytime in your setup method

✅ Closes: nuxt-community#64
@Mikaleb
Copy link

Mikaleb commented Nov 13, 2020

Error is not closed at all, can still reproduce with last version :

layouts/default.vue

export default defineComponent({
name: 'ContentLayout',
setup(props, ctx) {

    const { title } = useMeta()
    title.value = 'Test'
    return {title}
}
//...

Getting the error : useMeta must be called within a component.

@danielroe
Copy link
Member

@Mikaleb Would you mind providing a reproduction?

@Mikaleb
Copy link

Mikaleb commented Nov 13, 2020

@danielroe I tried and saw that my version was not updated for some reason, my apologies !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants