1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <!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" />
- <link rel="icon" href="../../logo.svg" />
- <title>天气案例_监视属性</title>
- </head>
- <body>
- <div id="root">
- <h2>今天天气很{{info}}</h2>
- <button @click="isHot = !isHot">切换天气</button>
- </div>
- <script src="../../vue.js"></script>
- <script>
- Vue.config.productionTip = false; // 阻止 vue 在启动时生成生产提示。
- const vm = new Vue({
- el: "#root",
- data: {
- isHot: true,
- },
- methods: {
- // changeWeather() {
- // this.isHot = !this.isHot;
- // },
- },
- computed: {
- info() {
- return this.isHot ? "炎热" : "凉爽";
- },
- },
- // watch: {
- // isHot: {
- // immediate: true, // 初始化时让 handler 调用一下
- // handler(currentValue, prevValue) {
- // console.log(currentValue, prevValue);
- // },
- // },
- // },
- });
- vm.$watch("isHot", {
- immediate: true, // 初始化时让 handler 调用一下
- handler(currentValue, prevValue) {
- console.log(currentValue, prevValue);
- },
- });
- </script>
- </body>
- </html>
|