美文网首页
[react-router.v4] Uncaught TypeE

[react-router.v4] Uncaught TypeE

作者: 德拉T | 来源:发表于2019-02-22 17:24 被阅读0次
    1. 选择 react-router 和 react-router-dom
      只需引用一个,两者之处就是dom多了<Link> <BrowserRouter>这样的 DOM 类组件

    2. 报错 Uncaught TypeError: Cannot read property 'location' of undefined

    impt 引用时写

    import React from "react";
    import { render } from "react-dom";
    
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    
    const Home = () => {
      return (
        <div>
          <h1>test home</h1>
        </div>
      );
    };
    
    const about = () => {
      return (
        <div>
          <h1>test about</h1>
        </div>
      );
    };
    
    const topics = () => {
      return (
        <div>
          <h1>test topics</h1>
        </div>
      );
    };
    
    render(
      <Router>
        <div>
          <ul>
            <li>
              <Link to="/">首页</Link>
            </li>
            <li>
              <Link to="/about">关于</Link>
            </li>
            <li>
              <Link to="/topics">主题列表</Link>
            </li>
          </ul>
    
          <Route exact path="/" component={Home} />
          <Route path="/about" component={about} />
          <Route path="/topics" component={topics} />
        </div>
      </Router>,
    
      document.getElementById("root")
    );
    
    

    相关文章

      网友评论

          本文标题:[react-router.v4] Uncaught TypeE

          本文链接:https://www.haomeiwen.com/subject/smoeyqtx.html