我在 YML 中使用 HTML 生成 [Object Object]
context:
fieldLabel: |
<strong>
Some Bold Text
</strong> and now normal continued.
渲染为
context:
fieldLabel: ",[object Object], and now normal continued.↵"
但我希望我的输出是
context: { fieldLabel: '<strong>\n Some Bold Text\n</strong> and now normal continued.\n' },
我的 JS 代码:
const tests = YAML.safeLoad(this.props.children,{json:true});
console.log("tests",...tests)
控制台输出:
context:
fieldLabel: ",[object Object], and now normal continued.↵"
它以 [object Object] 的形式出现,而不是 <strong>\n Some Bold Text\n</strong>
最佳答案
来自js-yaml docs :
safeLoad (string [ , options ])
Recommended loading way. Parses string as single YAML document. Returns a JavaScript object or throws YAMLException on error. By default, does not support regexps, functions and undefined. This method is safe for untrusted data.
由于您在 yaml 中包含非安全代码(即 HTML),因此 js-yaml 似乎给您带来了错误。如果您的 yaml 保证安全,您可以使用 load()
进行非安全加载,或者您可以修改代码结构以更改 yaml 中存储的内容和不存储的内容.
关于javascript - 在 YAML 中使用 HTML 会生成 js-yaml 中的 [Object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797217/