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

How to use it with Vuejs 3 + TypeScript ? #665

Closed
ayoubkhan558-zz opened this issue Apr 5, 2021 · 18 comments
Closed

How to use it with Vuejs 3 + TypeScript ? #665

ayoubkhan558-zz opened this issue Apr 5, 2021 · 18 comments
Labels

Comments

@ayoubkhan558-zz
Copy link

How to use it with Vuejs 3 + TypeScript ?

@ayoubkhan558-zz
Copy link
Author

Argument of type 'typeof import("E:/Ayoub Khan/Porfolios/ayoubkhan-portfolio/node_modules/vue-meta/dist/vue-meta")' is not assignable to parameter of type 'Plugin_2'.
  Property 'install' is missing in type 'typeof import("E:/Ayoub Khan/Porfolios/ayoubkhan-portfolio/node_modules/vue-meta/dist/vue-meta")' but required in type '{ install: PluginInstallFunction; }'.ts(2345)

image

@mixalbl4-127
Copy link

mixalbl4-127 commented Apr 11, 2021

  1. Make shure that you use vue-meta@3.0.0-alpha.4
  2. main.ts
import { createMetaManager } from "vue-meta";
...
const metaManager = createMetaManager();

app.use(metaManager).mount("#app");

@pimlie
Copy link
Collaborator

pimlie commented Apr 11, 2021

Just added a types entry in package.json, that was missing in versions before alpha.4.

@charles-allen
Copy link

charles-allen commented Apr 16, 2021

I needed to do 6 things:

  1. Update vue-meta to v3 (in package.json)

    - "vue-meta": "^2.4.0",
    + "vue-meta": "^3.0.0-alpha.4",

    ...or with yarn:

    yarn add vue-meta@alpha
  2. Add metaManager to Vue app

    const app = createApp(App)
      .use(router)
      .use(store)
      .use(createMetaManager()) // add this line
    
    await router.isReady()
    app.mount('#app')
  3. Add <metainfo> to App.vue <template> (this is also where I set a "title template")

    <template>
      <metainfo>
        <template v-slot:title="{ content }">{{ content ? `${content} | SITE_NAME` : `SITE_NAME` }}</template>
      </metainfo>
      <header />
      <router-view />
      <footer />
    </template>
  4. Set default meta in App.vue <script>
    Vue 3 vanilla:

    export default {
      setup () {
        useMeta({
          title: '',
          htmlAttrs: { lang: 'en', amp: true }
        })
      }
    }

    or with vue-class-component:

    export default class App extends Vue {
      meta = setup(() => useMeta({
        title: '',
        htmlAttrs: { lang: 'en', amp: true }
      })
    }
  5. Override meta in each component
    Vue 3 vanilla:

    export default {
      setup () {
        useMeta({ title: 'Some Page' })
      }
    }

    or with vue-class-component:

    export default class SomePage extends Vue {
      meta = setup(() => useMeta({ title: 'Some Page' }))
    }
  6. Comment out the title in /public/index.html (see Issue #667) Fixed in alpha.7

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />
        <!-- <title><%= htmlWebpackPlugin.options.title %></title> -->
        ...
      </head>
      ...
    </html>

@pimlie - Please correct me if I missed something or if a step is unnecessary!

@pimlie
Copy link
Collaborator

pimlie commented Apr 18, 2021

@charles-allen Thanks for the detailed log, this seems to be ok. Returning metadata: useActiveMeta shouldnt be strictly necessary though, in the examples its being returned to print the active meta state on the page as a stringified json object. But maybe you are using metadata yourself outside of the metainfo componnet?

@charles-allen
Copy link

Thanks for the feedback! I've updated my project & fixed the example above. I've also added a vanilla Vue 3 version for people not using vue-class-component :)

@misterboe
Copy link

Have a look at the docs on the new alpha tag > https://github.com/nuxt/vue-meta/tree/v3.0.0-alpha.5

Works out of the box.

@Trinovantes
Copy link

alpha.5 moved ssr functionality to a separate file but it's currently missing declarations dist/ssr/index.d.ts

@ataylor32
Copy link

ataylor32 commented May 13, 2021

I'm working on a Vue 3 project that uses the options API with the help of defineComponent (in other words, this project does not use the composition API). I tried getting vue-meta v3.0.0-alpha.5 to work with this project, but the <title> wasn't reactive.

An example use case: let's suppose your route is /articles/some-article. Before the article is loaded, we only know the slug (some-article). Once it loads, we have all of the article's information, including its title. At that point, I would like the <title> to be set to the article's title. I haven't been able to get this working in my project and I'm wondering if it's because I'm not using the composition API.

@charles-allen
Copy link

@ataylor32 - I'm not using defineComponent, but I also had some issues with a dynamic title. For me, I think my mistake was not initializing my "article" field (leaving it as undefined); changing it to be initialized as null seems to have made the difference (presumably because that ensures it's reactive).

My code is using vue-class-component, but here's the relevant part anyway in case it triggers any other insights or useful discussion:

export default class Profile extends Vue.with(class {
  slug!: string
}) {
  user: User | null = null

  meta = setup(() => useMeta(computed(() => ({
    title: this.user?.name ?? 'Profile'
  }))))

  async mounted(): Promise<void> {
    this.user = (await db.collection(USERS).doc(this.slug).get()).data() as (User | undefined) ?? null
  }
}

@Sirius-KiH
Copy link

How would you use og or twitter meta tags with metaInfo?

<metainfo>
  <template v-slot:og:title="{ content }">{{ content }}</template>
</metainfo>

^^ This does not work.

Also, is there an elegant way to use the same value for multiple fields (like title, og:title and twitter:title)? Right now I would use something like this:

        useMeta({
                title: 'My title',
                "og:title": 'My title',
                "twitter:title": 'My title',
        });

@pimlie
Copy link
Collaborator

pimlie commented Jun 20, 2021

@Sirius-KiH With the default vue-meta configuration opengraph & twitter can be added by using nested objects.

description: description,
og: {
  description: ogDescription,
},
twitter: {
  description: twitterDescription,
}

then in the component you could do:

      <template v-slot:og(description)="{ content, metainfo }">{{ content || metainfo.description }}</template>
      <template v-slot:twitter(description)="{ content, metainfo }">{{ content || metainfo.description }}</template>

this way if ogDescription or twitterDescription are empty they will fallback to the default description

@stale
Copy link

stale bot commented Jul 21, 2021

Thanks for your contribution to vue-meta! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of vue-meta
  2. Comment the steps to reproduce it
    Issues that are labeled as pending will not be automatically marked as stale.

@stale stale bot added the stale label Jul 21, 2021
@romain130492
Copy link

romain130492 commented Oct 4, 2021

why Am i still getting that error...: Uncaught SyntaxError: The requested module '/node_modules/.vite/vue-meta.js?v=4865df67' does not provide an export named 'createMetaManager'?
I followed the exact steps from above

  • Vue3 project with no SSR and using Vite and using "vue-meta": "^3.0.0-alpha.4",

@stale stale bot removed the stale label Oct 4, 2021
@doutatsu
Copy link

@romain130492 You need to update the version - alpha.9 is the latest and I think plugin was added in alpha.8

@doutatsu
Copy link

What is the latest update on the Vue 3 support? Documentation is very sparse, not quite sure how to migrate the old version to this one

@stale
Copy link

stale bot commented Apr 17, 2022

Thanks for your contribution to vue-meta! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of vue-meta
  2. Comment the steps to reproduce it
    Issues that are labeled as pending will not be automatically marked as stale.

@tripflex
Copy link

tripflex commented Feb 7, 2024

For anybody showing up later searching, this project is no longer maintained ( #808 ) and suggest looking at using something like https://github.com/unjs/unhead/

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

No branches or pull requests