天气切换示例,本篇实现了vue版本、react版本。
vue实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<div id="root">
<h2>今天天气很{{isHot?'炎热':'凉爽'}}</h2>
<button @click="changeWeather">切换天气</button>
</div>
<script>
Vue.config.productionTip = false;
new Vue({
el:"#root",
data(){
return {
isHot:true
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot;
}
}
})
</script>
</body>
</html>
react实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo</title>
<script src="./js/react.development.js"></script>
<script src="./js/react-dom.development.js"></script>
<script src="./js/babel.min.js"></script>
</head>
<body>
<div id="root">
</div>
<script type="text/babel">
class Weather extends React.Component{
state = {
isHot:true
}
changeWeatcher = () => {
const {isHot} = this.state;
this.setState({
isHot:!isHot
})
}
render(){
const {isHot} = this.state;
const {changeWeatcher} = this;
return (
<div>
<h2>今天天气很{isHot?"炎热":"凉爽"}</h2>
<button onClick={changeWeatcher}>天气切换</button>
</div>
)
}
}
ReactDOM.render(<Weather/>,document.getElementById("root"));
</script>
</body>
</html>