【发布时间】:2019-08-07 23:39:59
【问题描述】:
根据官方方法:
将 React 添加到网站https://reactjs.org/docs/add-react-to-a-website.html
我用内容创建了test.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Add React in One Minute</title>
</head>
<body>
<h2>Add React in One Minute</h2>
<p>This page demonstrates using React with no build tooling.</p>
<p>React is loaded as a script tag.</p>
<!-- We will put our React component inside this div. -->
<div id="like_button_container"></div>
<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- Load our React component. -->
<script src="test.js"></script>
</body>
</html>
还有test.js:
'use strict';
const e = React.createElement;
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
componentDidMount() {
axios.get(`http://localhost:4000/api/v1/cars`)
.then(result => {
console.log(result.data[0].make);
})
}
render() {
if (this.state.liked) {
return 'You liked this.';
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);
上面的代码运行良好。
我可以按 Like 按钮并查看更改,还可以使用 Axios 等库。
现在我想打开 http://localhost/test.html?param1=111¶m2=222 并在 test.js 中获取这些 param1 和 param2 变量 - 反应。那可能吗?如何做到这一点?
非常感谢
【问题讨论】:
-
为什么要 react 改变访问查询参数的方式? stackoverflow.com/questions/901115/…
标签: javascript html reactjs