【问题标题】:Can i use react w/o a server and as a js file?我可以在没有服务器的情况下使用 react 并作为 js 文件吗?
【发布时间】:2020-07-24 20:31:58
【问题描述】:

基本上我有一个文件,它生成一些数据并将其写入一个基本的 html 文件。我制作了一个简单的 React 应用程序来更好地解析和显示数据,但如果我每次都必须运行服务器,它就没有用了。我真正需要的是一个快捷方式 html 页面,它可以独立运行所有反应。希望这是有道理的。

【问题讨论】:

    标签: html reactjs


    【解决方案1】:

    您正在寻找客户端的静态模板引擎。

    但是,要回答您的问题,是的,这绝对是可能的。但是您只需要包含所需的依赖项。另请注意,这未针对生产进行优化

    <!-- The root where React App gets rendered. -->
    <div id="root"></div>
    
    <!-- React JS and React DOM Libraries. Add Babel for transpiling, as you might use JSX. -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    
    <!-- The real React JSX App -->
    <script type="text/babel">
    class App extends React.Component {
      render() {
        return (
          <div>
            This is React!
          </div>
        );
      }
    }
    
    ReactDOM.render(
      <App />,
      document.getElementById('root')
    );
    </script>
    

    【讨论】:

      【解决方案2】:

      听起来像是静态 HTML 的生成。在这种情况下你不需要 React。只需使用 pug 或您选择的其他模板引擎。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-27
        • 1970-01-01
        • 2022-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多