vue中computed和watch的使用实例代码解析
更新时间:2022年04月02日 10:20:13 作者:wanglingli95
这篇文章主要介绍了vue中computed和watch的综合运用实例,主要需求是点击按钮实现天气的切换效果结合示例代码给大家介绍的非常详细,需要的朋友可以参考下
需求:
- 1.点击按钮实现天气的切换;
- 2.用watch进行监视天气产生变化的数据;


实现代码(helloworld.vue实现代码):
<template>
<!-- 准备好一个容器-->
<div id="root">
<h2>今天天气很{{info}}</h2>
<button @click="changeWeather">切换天气</button>
</div>
</template>
<script>
export default {
name:'HelloWorld',
data(){
return{
isHot:true,
}
},
computed:{
info(){
return this.isHot ? '炎热' : '凉爽'
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
watch:{
isHot(val){
console.log("isHot被修改了,isHot值为:",val)
}
}
</script>
<style>
</style>注意:watch监听的对象都是在data()中已经定义好的数据。
到此这篇关于vue中computed和watch的综合运用实例的文章就介绍到这了,更多相关vue computed和watch运用实例内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
在vue-cli脚手架中配置一个vue-router前端路由
这篇文章主要给大家介绍了在vue-cli脚手架中配置一个vue-router前端路由的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。2017-07-07


最新评论