Ric*_*tos 4 html javascript jsx reactjs react-redux
想要在我的模板中直接显示 html 标记。
这是我正在编写要显示的 html 代码的文件。我想显示所有的 html 元素。
import React from 'react';
const html = (
<div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
);
export default html
Run Code Online (Sandbox Code Playgroud)
组件文件
import React, { Component } from 'react';
import html from './code/htmlCodeSnippet';
...
render() {
return (
<div>
{html}
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
我正在导入要在我的组件中显示的 html 文件。我将使用 html 荧光笔来显示代码。
每当我引用 时{html},它都不会显示所有 html 元素。它只是显示为
1
2
3
Run Code Online (Sandbox Code Playgroud)
你需要做一些改变。首先更改html为字符串即
const html = `
<div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<p>hello</p>
</div>`;
Run Code Online (Sandbox Code Playgroud)
并使用pre和code元素来包装html渲染方法,即
<pre><code>{html}</code></pre>