【问题标题】:Import html string on a different file on react. Using prismjs to display code highlighting在反应时在不同的文件上导入 html 字符串。使用prismjs显示代码高亮
【发布时间】:2019-03-26 21:37:01
【问题描述】:

在设计系统中使用 Prismjs 显示代码 sn-ps。

我想将 html 代码示例分离到一个独立文件中并将其导入到我的组件中。

代码示例组件:

CodeSampleContainer.jsx

import React, { Component } from 'react';
import Prism from "prismjs";
import './CodeSample.scss';
import '../Shared/prism.css';

// Import separate html file
import { html } './htmlSnippet.jsx';

class CodeSample extends Component {
  hasHtmlBlob() {
    return (
      <pre>
        <code className="language-html">
          {html} // Any html displayed here will be highlighted by prism
        </code>
      </pre>
      )
    }
  }

  render() {
    return (
      <div className="CodeSample"> 
        {this.hasHtmlBlob()}
      </div>
    )
  }
}

我要导出的 HTML:

htmlSnippet.jsx

const html = `
  <div>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </div>`

return html;

【问题讨论】:

    标签: javascript html reactjs prismjs


    【解决方案1】:

    你的代码有两个问题:

    JSX 语法

    不要将模板声明为字符串,而应以“反应方式”进行

    const html = (
        <div>
            <ul>
               <li>1</li>
               <li>2</li>
               <li>3</li>
            </ul>
        </div>
    );
    

    缺少导出

    如果您想从htmlSnippet.jsx 导出模板,您应该使用export,而不是return

    export { html };
    

    【讨论】:

    • 我在尝试显示 {html} 时遇到问题。它以 [object object] 的形式返回
    猜你喜欢
    • 2017-04-20
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多