繁体   English   中英

将内容从html转换为ReactJS文件

[英]Converting contents from html to ReactJS file

如果问题令人困惑,我们深表歉意。 基本上我有这个html代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A Gentle Introduction</title>

    <script
      src="https://rawgit.com/flatiron/director/master/build/director.min.js">
    </script>

    <script>
      var author = function () { console.log("author"); };
      var books = function () { console.log("books"); };
      var viewBook = function (bookId) {
        console.log("viewBook: bookId is populated: " + bookId);
      };

      var routes = {
        '/author': author,
        '/books': [books, function() {
          console.log("An inline route handler.");
        }],
        '/books/view/:bookId': viewBook
      };

      var router = Router(routes);

      router.init();
    </script>
  </head>

  <body>
    <ul>
      <li><a href="#/author">#/author</a></li>
      <li><a href="#/books">#/books</a></li>
      <li><a href="#/books/view/1">#/books/view/1</a></li>
    </ul>
  </body>
</html>

显然在.html文件中。 我想将其更改为.js文件,以便可以在js中放入html,以便在单击不同链接时,路由/返回的内容不同。

我真的不知道如何直接将其放入javascript文件中,然后使路由器工作。 这是html文件来自https://github.com/flatiron/director#client-side-routing的位置 ,我正在尝试使用此Flatiron / director路由器。

任何帮助将是巨大的!

我能够使其与react和jsx一起工作,而外部的路由代码也可以自身响应。

与es6 / es2015一起编写

app.js

const author = () => { console.log("author"); };
const books = () => { console.log("books"); };
const viewBook = (bookId) => { console.log("viewBook: bookId is populated: " + bookId); };

const routes = {
  '/author': author,
  '/books': [books, () => { console.log("An inline route handler."); }],
  '/books/view/:bookId': viewBook
};

const router = Router(routes);

router.init();

class SampleRouting extends React.Component {
  render() {
    return (
      <ul>
        <li><a href="#/author">#/author</a></li>
        <li><a href="#/books">#/books</a></li>
        <li><a href ="#/books/view/1">#/books/view/1</a></li>
      </ul>
    )
  }
}

React.render( <SampleRouting/> , document.getElementById('root'));

index.html

<div id="root"></div>

范例: http //s.codepen.io/oobgam/debug/vNoogO

_edit app.js以反映状态和页面标题的更新

class App extends React.Component {
 constructor(props) {
    super(props);
    this.state = { currentPage: 'author' }
 }

  componentDidMount() {
    const author = () => { this.setState({currentPage: 'author'}) };
    const books = () => { this.setState({currentPage: 'Books'}); };
    const viewBook = (bookId) => { this.setState({currentPage: 'Book ' + bookId }); };
    const routes = {
      '/author': author,
      '/books': books,
      '/books/view/:bookId': viewBook
    };
    const router = Router(routes);
    router.init();
  }

  render() {
    return (
      <div>
        <h1>{ this.state.currentPage }</h1>
        <SampleRouting />
      </div>
    );
  }
}

// stateless function
const SampleRouting = () => {
   return (
      <ul>
        <li><a href="#/author">#/author</a></li>
        <li><a href="#/books">#/books</a></li>
        <li><a href ="#/books/view/1">#/books/view/1</a></li>
      < /ul>
    )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM