0

使用 Prismjs 在设计系统中显示代码片段。

我想将 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;
4

1 回答 1

2

你的代码有两个问题:

JSX 语法

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

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

出口丢失

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

export { html };
于 2019-03-26T23:58:39.577 回答