【问题标题】:useMeta not updating title in Nuxt Composition APIuseMeta 未更新 Nuxt Composition API 中的标题
【发布时间】:2021-04-06 07:43:35
【问题描述】:

这是我在组件脚本中的代码:检查时标题标签不显示

import {  useMeta } from "@nuxtjs/composition-api";

export default {
 components: {  }, 

head: {},

setup() {

useMeta({
        title: 'My title',
        meta: [
            {
                hid: 'description',
                name: 'description',
                content: 'My description',
            },
        ],
    })


const screenType = ref("desktop");
var deviceType = ""
// const screenType = ref("mobile")
// const screenType = ref("landscape")

if (process.browser) {
    window.onNuxtReady(() => {
        if (window.innerWidth < 500) {
        screenType.value = "mobile";
        } else {
        screenType.value = "desktop";
        }

       
        

        if (navigator.userAgent.match(/mobile/i)) {
            deviceType =  "mobile";
        } else if (navigator.userAgent.match(/iPad|Android|Touch/i)) {
            deviceType = "tablet";
        } else {
            deviceType = "desktop";
        }


    })
 }



} } 

我的nuxt.config.js中没有头对象

我在这里错过了什么?

【问题讨论】:

    标签: vue.js nuxt.js meta-tags


    【解决方案1】:

    在您的页面组件的设置功能中使用 defineComponent

    export default defineComponent({
        head: {}, // Needed in nuxt 2
        setup() {
            const { title, meta } = useMeta()
            title.value = 'My title'
            meta.value = [
                {
                hid: 'description',
                name: 'description',
                content:
                    'My description',
                },
            ]
        },
      })
    

    https://composition-api.nuxtjs.org/packages/useMeta

    【讨论】:

    • 我试过了,好像不行。我可以记录标题和元值,但它没有在浏览器中更新。我在 dev 上运行它,它只适用于 build 吗?
    • 确保只在页面组件上使用它
    • 这是我的文件结构 > 页面 > 小部件 > _id.vue。我的代码在 _id.vue
    • 你还需要使用defineComponent
    【解决方案2】:

    你应该使用从“@nuxtjs/composition-api”导入的defineComponent。不要使用从“@vue/composition-api”导入的defineComponent。

    【讨论】:

    • 阅读How to Answer:你能解释一下为什么应该使用前者而不是后者吗?
    • 前者要统一使用,因为如果两者一起使用会导致不兼容导致失效。
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2021-09-17
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多