微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 json-ld 添加到 Vue 3?

如何解决如何将 json-ld 添加到 Vue 3?

尝试将 json-ld 脚本标记添加到传送到 HEAD 的部分时,Vue 3 编译器失败并显示以下错误

VueCompilerError: Tags with side effect (<script> and <style>) are ignored in client component templates.

模板:

<template>
  <teleport to="head">
    <script type="application/ld+json">
      {
        "@context": "https://schema.org","@type": "WebSite","url": "https://www.example.com/","potentialAction": {
          "@type": "SearchAction","target": "https://query.example.com/search?q={search_term_string}","query-input": "required name=search_term_string"
        }
      }
    </script>
  </teleport>
  <div>
    Hello World!
  </div>
</template>

如何在不安装其他依赖项的情况下添加这个 json-ld 标签

解决方法

一种解决方法是使用 Vue 的 built-in component(即 <component :is="'script'">)而不是 <script>

<template>
  <teleport to="head">
    <component :is="'script'" type="application/ld+json">
      {
        "@context": "https://schema.org","@type": "WebSite","url": "https://www.example.com/","potentialAction": {
          "@type": "SearchAction","target": "https://query.example.com/search?q={search_term_string}","query-input": "required name=search_term_string"
        }
      }
    </component>
  </teleport>
  <div>
    Hello world!
  </div>
</template>

demo

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。