【发布时间】: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="{
"target": "#footerLanguage",
"type": "css-animation",
"animationIn": "slideInDown"
}" 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