【问题标题】:Nuxt.js changes formatting on json data in data attribute depending on nestingNuxt.js 根据嵌套更改数据属性中 json 数据的格式
【发布时间】:2021-04-07 06:39:16
【问题描述】:

我有以下简单的 vue 组件,其属性 data-hs-go-to-options 包含 json 数据。

<template>
  <div>
    <a
      class="js-go-to go-to position-fixed"
      href="javascript:;"
      style="visibility: hidden"
      data-hs-go-to-options='{
          "offsetTop": 700,
          "position": {
          "init": {
              "right": 50
          },
          "show": {
              "bottom": 15      
          },
          "hide": {
              "bottom": -15
          }
          }
          }'
        >
      <i class="fas fa-angle-up"></i>
    </a> 
  </div>
</template>

当我构建 nuxt 应用程序并尝试使用以下命令获取数据属性时

$('.js-go-to').attr('data-hs-go-to-options')

我收到这个:

"{
        "

当我从上面的模板中删除 div 元素,再次构建并运行命令时,我得到了正确的内容:

"{
      "offsetTop": 700,
      "position": {
      "init": {
          "right": 50
      },
      "show": {
          "bottom": 15      
      },
      "hide": {
          "bottom": -15
      }
      }
      }"

使用 div 渲染的网站内容如下所示:

<div><a href="javascript:;" data-hs-unfold-options="{
                    "target": "#footerLanguage",
                    "type": "css-animation",
                    "animationIn": "slideInDown"
                    }" class="js-hs-unfold-invoker dropdown-toggle btn btn-xs btn-soft-secondary"></a></div>

没有像这样的 div:

<a href="javascript:;" data-hs-unfold-options="{
                  &quot;target&quot;: &quot;#footerLanguage&quot;,
                  &quot;type&quot;: &quot;css-animation&quot;,
                  &quot;animationIn&quot;: &quot;slideInDown&quot;
                  }" class="js-hs-unfold-invoker dropdown-toggle btn btn-xs btn-soft-secondary"></a>

我不知道为什么会这样,有人可以帮忙吗?

【问题讨论】:

  • 我对 nuxt.js 没有具体的答案,但出于这个确切原因,我强烈建议您在 将其插入 html 之前对您的 json 进行 url 编码。我非常愿意打赌,如果您使用编码字符串以使其无论哪种方式都是有效的 html,您将获得更加一致的结果,并且它就像在将其解析为 json 之前解析 url 编码字符串一样简单

标签: javascript vue.js nuxt.js


【解决方案1】:

Vue 在这种情况下将您的 JSON 视为 string

<template>
  <div>
    <a
      class="js-go-to go-to position-fixed"
      href="javascript:;"
      style="visibility: hidden"
      data-hs-go-to-options='{
          "offsetTop": 700,
          "position": {
          "init": {
              "right": 50
          },
          "show": {
              "bottom": 15      
          },
          "hide": {
              "bottom": -15
          }
          }
          }'
        >
      <i class="fas fa-angle-up"></i>
    </a> 
  </div>
</template>

通过在指令前添加: 将数据绑定到元素,因此您告诉Nuxt它应该是object,而不是string

<template>
  <div>
    <a
      class="js-go-to go-to position-fixed"
      href="javascript:;"
      style="visibility: hidden"
      :data-hs-go-to-options='{
        "offsetTop": 700,
        "position": {
          "init": {
            "right": 50
          },
          "show": {
            "bottom": 15      
          },
          "hide": {
            "bottom": -15
          }
        }
      }'
    >
      <i class="fas fa-angle-up"></i>
    </a> 
  </div>
</template>

如果你在 jQuery 中使用它,那么首先使用JSON.stringify(),所以所有内容都会根据需要进行转义:

<template>
  <div>
    <a
      class="js-go-to go-to position-fixed"
      href="javascript:;"
      style="visibility: hidden"
      :data-hs-go-to-options='JSON.stringify({
        "offsetTop": 700,
        "position": {
          "init": {
            "right": 50
          },
          "show": {
            "bottom": 15      
          },
          "hide": {
            "bottom": -15
          }
        }
      })'
    >
      <i class="fas fa-angle-up"></i>
    </a> 
  </div>
</template>

【讨论】:

  • 带有 JSON.stringify 的指令之前的 ':' 有效。非常感谢!尽管我仍然很困惑为什么有时将其视为字符串而有时不视为字符串。
  • @titech 它始终被视为string除非您进行绑定(v-bind:something 或简单的:something)。不同之处在于,在某些情况下 string 有效,在某些情况下需要其他一些 JS 格式(如 ObjectArray 等)。因此,将花括号 ({ }) 包裹在某物周围是不够的 - 如果您不说的话,即使是大括号也将是 strings,它应该被评估为 JS :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多