是否可以仅将React和reactDom用作html中的标签来使用React.Component?

时间:2019-05-03 20:15:00

标签: javascript reactjs axios cdn

我一直在寻找答案并阅读文档,但我仍然不清楚。因此,我将使其变得简单。我想知道是否有可能使用React.Component,例如将react和reactDOM作为标记包含在我的html中。

我怀疑自己没有工作,因为我已经尝试过使用id。这是一段代码:

    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
        user: []
        }
       }



      componentDidMount() {
         var _this = this;
         axios.get('http://localhost:8080/dashboard?user=' + 
         localStorage.getItem("user"))
        .then(function(res){
          _this.setState({
           items: res.data.user
           });
         })
        .catch(function(e) {
           console.log("ERROR ", e);
        })
      }

    }

已编辑

html:

        <!DOCTYPE html>
        <html lang="en">
        <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!-- Meta, title, CSS, favicons, etc. -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="../css/main.css" rel="stylesheet">
        <script src="js/jquery-3.3.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js 
        " integrity="sha384- 
        ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 
         crossorigin="anonymous"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/handlebars-v4.0.12.js"></script>
        <script type="application/javascript" src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
        <script type="application/javascript" src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
        <script src="js/home.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/reactstrap/4.8.0/reactstrap.min.js"></script>
       </head>
       <body class="nav-md">
       </body>
       </html>

正如我所阅读的,它是假设在随后加载html时,它会执行react代码,导致它具有de React.Component,然后应该调用componentDidMount并执行请求,但我看不到会发生这种情况

有什么想法吗?

谢谢!

0 个答案:

没有答案