01.天气案例
<!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>Document</title>
<script src="../JS/vue.js"></script>
</head>
<body>
<div id='root'>
<h2>今天的天气很{{info}}</h2>
<!-- 如果绑定事件的任务比较简单,我们不需要methods,可以在后面直接写,多个不建议,例如: -->
<!-- <button @click='isHot=!isHot'>点我切换</button> -->
<button @click='changeWeather'>点我切换</button>
</div>
<script>
Vue.config.productionTip = false;
new Vue({
el: '#root',
data: {
isHot: true
},
computed: {
info() {
return this.isHot ? '炎热' : '凉爽'
}
},
methods: {
changeWeather() {
this.isHot = !this.isHot
}
},
})
</script>
</body>
</html>
02.天气案例-监视属性
<!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>Document</title>
<script src="../JS/vue.js"></script>
</head>
<body>
<div id='root'>
<h2>今天的天气很{{info}}</h2>
<!-- 如果绑定事件的任务比较简单,我们不需要methods,可以在后面直接写,多个不建议,例如: -->
<!-- <button @click='isHot=!isHot'>点我切换</button> -->
<button @click='changeWeather'>点我切换</button>
</div>
<script>
Vue.config.productionTip = false;
const vm = new Vue({
el: '#root',
data: {
isHot: true
},
computed: {
info() {
return this.isHot ? '炎热' : '凉爽'
}
},
methods: {
changeWeather() {
this.isHot = !this.isHot
}
},
// 以下是新内容:监视属性
// 1.当监视属性发生变化时,回调函数自动调用,进行相关操作。
// 2.监视属性必须存在
// watch: { // watch有监视的意思。
// isHot: { // 监视谁呢?这里监视isHot,info也能被监视。
// // immediate 初始化时,让handler调用一下,默认是false
// immediate: true,
// handler(newValue, oldValue) { // 参数有变动前 和 变动后的参数
// console.log('isHot被修改', newValue, oldValue);
// }
// }
// }
})
// 另一种写法格式,如下
// vm.$watch('监视对象', {})
vm.$watch('isHot', {
immediate: true,
handler(newValue, oldValue) { // 参数有变动前 和 变动后的参数
console.log('isHot被修改', newValue, oldValue);
}
})
</script>
</body>
</html>
03.深度检测
<!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>Document</title>
<script src="../JS/vue.js"></script>
</head>
<body>
<div id='root'>
<h2>a的值是:{{numbers.a}}</h2>
<!-- 因为需求比较简单且只有一个需求,可以直接书写 -->
<button @click='numbers.a++'>点我让a++</button>
<h2>b的值是:{{numbers.b}}</h2>
<button @click='numbers.b++'>点我让b++</button>
<!-- 证明deep:true是可以监测多层的。 -->
<h2>e的值是:{{numbers.c.d.e}}</h2>
<button @click='numbers.c.d.e++'>点我让e++</button>
</div>
<script>
Vue.config.productionTip = false;
const vm = new Vue({
el: '#root',
data: {
numbers: {
a: 1,
b: 2,
c: {
d: {
e: 8
}
}
}
},
watch: { // watch有监视的意思。
numbers: {
// 深度检测
// Vue中的watch默认不监测对象内部值的改变(一层)
// 配置deep:true,可以监测多层,deep默认是false
// 备注:
// Vue自身可以监测对象内部值的改变,但是Vue提供的watch不支持
// 使用watch时,根据数据的要求,决定是否采用深度监视
deep: true,
handler() {
console.log('numbers被修改');
}
}
}
})
</script>
</body>
</html>
04.天气案例-监视属性简写
<!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>Document</title>
<script src="../JS/vue.js"></script>
</head>
<body>
<div id='root'>
<h2>今天的天气很{{info}}</h2>
<!-- 如果绑定事件的任务比较简单,我们不需要methods,可以在后面直接写,多个不建议,例如: -->
<!-- <button @click='isHot=!isHot'>点我切换</button> -->
<button @click='changeWeather'>点我切换</button>
</div>
<script>
Vue.config.productionTip = false;
const vm = new Vue({
el: '#root',
data: {
isHot: true
},
computed: {
info() {
return this.isHot ? '炎热' : '凉爽'
}
},
methods: {
changeWeather() {
this.isHot = !this.isHot
}
},
// 以下是新内容:监视属性
// 1.当监视属性发生变化时,回调函数自动调用,进行相关操作。
// 2.监视属性必须存在
// watch: { // watch有监视的意思。
// isHot: { // 监视谁呢?这里监视isHot,info也能被监视。
// // immediate 初始化时,让handler调用一下,默认是false
// immediate: true,
// handler(newValue, oldValue) { // 参数有变动前 和 变动后的参数
// console.log('isHot被修改', newValue, oldValue);
// }
// }
// }
})
// 另一种写法格式,如下
// vm.$watch('监视对象', {})
// vm.$watch('isHot', {
// immediate: true,
// handler(newValue, oldValue) { // 参数有变动前 和 变动后的参数
// console.log('isHot被修改', newValue, oldValue);
// }
// })
// 再次简写(如果你不需要immediate和deep(深度检测)的时候)
vm.$watch('isHot', function (nowValue, oldValue) {
console.log('isHot被修改', nowValue, oldValue);
})
</script>
</body>
</html>
05.姓名案例-监视属性语法
<!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>姓名案例-methods值语法</title>
<!-- 创建新的页面,记得引入。 -->
<script type='text/javascript' src="../JS/vue.js"></script>
</head>
<body>
<div id="root">
<!-- 双向数据绑定使用v-model,可省略value -->
姓:<input type="text" v-model:value="firstName"><br><br>
名:<input type="text" v-model="lastName"><br><br>
姓名:<span>{{fullName}}</span>
</div>
<script>
Vue.config.productionTip = false;
const vm = new Vue({
el: '#root',
data: {
firstName: 'Vue',
lastName: '学习',
// 下面要先设置一个初始的,不然页面姓名内容是空白的。
fullName: 'Vue - 学习'
},
computed: {
// 完整写法
// fullName: {
// // get什么作用?
// // 答:当有人读取get的时候,且返回值是fullName的值。
// // get什么时候被调用?
// // 答:1.初次被调用
// // 2.所依赖的参数数据发生变化时。
// // 上面两点是与众不同之处,别的方法在多次调用的时候,系统会有缓存,在次调用会直接拿缓存的数据,而computed在发生变化的时候也会调用。
// // 优势:与methods相比,内部有缓存机智(复用),效率更高,调试方便
// get() {
// // 同样,不能写箭头函数
// return this.firstName + '-' + this.lastName
// },
// // 在你确定name不会发生改变的时候,可不写set
// set() {
// // 因为姓 名中间有个-,所有我们要提取。
// const arr = value.split('-')
// // 我们利用set修改之后,让名字和修改的内容相等。
// // firstName 和 lastName = value的值,会到前台显示,而fullname捕捉到了name发生变化,因此名字组合也会跟着变化
// firstName = value[0]
// lastName = value[1]
// }
// }
// 简写(当我们确定只读取不修改才能简写)
// fullName: function () {
// return this.firstName + '-' + this.lastName
// }
// 再次简写
// fullName() {
// return this.firstName + '-' + this.lastName
// }
},
watch: {
// 完整写法
// firstName: {
// // 因为我们不需要deep和深度检测,所以handler可不写.
// handler(newValue, oldValue) {
// // 此时我们只用到新的newvalue值,所以oldValue可不写,而newvalue也可简写val
// this.fullName = newValue + '-' + this.lastName
// }
// },
// lastName: {
// handler(newValue, oldValue) {
// this.fullName = this.firstName + '-' + newValue
// }
// }
// 简写(省去handler,只保留新的value,简写val)
// firstName(val) {
// this.fullName = val + '-' + this.lastName
// },
// lastName(val) {
// this.fullName = this.firstName + '-' + val
// }
// 拓展知识点:
// 在watch和计算属性都能完成的时候, 我们推荐计算属性完成.
// 但是涉及到异步定时器的时候,我们只能通过watch完成。
// 例如:我们要求在修改名字,1s之后在执行。
firstName(val) {
// 特别注意:此处我们用了箭头函数,this指向的是window
// 由Vue管理的函数,我们写普通函数,this指向的是vm或者组件实例对象。
// 不被Vue管理的函数,我们可以写箭头函数,例如:定时器,ajax回调函数,promise的回调函数
setTimeout(() => {
this.fullName = val + '-' + this.lastName
}, 1000)
},
lastName(val) {
setTimeout(() => {
this.fullName = this.firstName + '-' + val
}, 1000)
}
}
})
</script>
</body>
</html>