vue watch和computed的使用场景
watch 监听某个数据的变化(监听完调用什么函数) 一个数据影响多个数据 (比如:浏览器自适应、监控路由对象、监控自身属性变化)

watch: {
firstName(val) { this.fullName = val + this.lastName }
}
computed: {
fullName() { this.firstName + this.lastName; }
}

watch 场景:
1、自适应浏览器(监听浏览器宽高、如果有变化就存在localStorage里面去,或者有变化就通知其他组件改变化)

data() {
return {
height: ''
}
},
mounted() {
const _this = this
this.height = document.documentElement.clientHeight
localStorage.setItem('whVal', JSON.stringify({'height': this.height }))
window.onresize = function temp() {
_this.height = document.documentElement.clientHeight
}
},
watch: {
// 如果发生改变,这个函数就会运行
height() {
this.changeFixed(this.width, this.height)
// eventBus.$emit('onresize', {'height': this.height }) 或者通知其他组件变化
}
},
methods: {
changeFixed(width, height) { // 动态修改样式
localStorage.setItem('Layout', JSON.stringify({'height': height }))
}
}

2、监控路由对象

new Vue({
el: '#app',
router: router, //开启路由对象
watch: {
'$route': function(newroute, oldroute) {
console.log(newroute, oldroute);
//可以在这个函数中获取到当前的路由规则字符串是什么
//那么就可以针对一些特定的页面做一些特定的处理
}
}
})

computed 场景:
1、作为过滤器:展开更多

<li v-for="(item,index) in addressListFilter" :class="{'check':checkIndex == index}" @click="checkIndex=index;selectedAddrId=item._id"></li>
<a @click="expand" v-bind:class="{'open':limit>3}">展开更多</a>
data(){
return {
addressList:[], // 地址列表
limit:3, // 限制默认显示3个地址
checkIndex:0
}
},
computed:{
addressListFilter(){
return this.addressList.slice(0,this.limit);
}
},
methods:{
expand(){ // 点击more更多
if(this.limit ==3){
this.limit = this.addressList.length;
}else{
this.limit =3;
}
}
}
}

2、作为过滤器:tab切换

<span v-for="(item,index) in navList" :key="index" @click="type = index" :class="{'active':type===index}">{{item}}</span>
<li v-for="(item,index) in taskListfilter" :key="index">
</li>
data() {
return {
navList: ['全部', '实时单', '竞价单'],
type:0,
taskList:[]
}
},
computed: {
taskListfilter() {
switch (this.type) {
case 0:return this.taskList
case 1:return this.taskList.filter(item => item.type === '实时单')
case 2:return this.taskList.filter(item => item.type === '竞价单')
// default :return this.taskList
}
}
}
vue watch和computed的使用场景的更多相关文章
- Vue.js中 computed 和 methods 的区别
官方文档中已经有对其的解释了,在这里把我的理解记录一下Vue中的methods.watch.computed computed 的使用场景 HTML模板中的复杂逻辑表达式,为了防止逻辑过重导致不易维护 ...
- vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)
_ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...
- 实例分析Vue.js中 computed和methods不同机制
在vue.js中,有methods和computed两种方式来动态当作方法来用的 1.首先最明显的不同 就是调用的时候,methods要加上() 2.我们可以使用 methods 来替代 comput ...
- vue Watcher分类 computed watch
1.Watcher构造函数源码部分代码 if (options) { this.deep = !!options.deep this.user = !!options.user this.lazy = ...
- Vue 中的 computed 和 methods
Vue 中的 computed 和 methods 使用 computed 性能会更好. 如果你不希望缓存,可以使用 methods 属性.
- vue中methods,computed,filters,watch的总结
08.28自我总结 vue中methods,computed,filters,watch的总结 一.methods methods属性里面的方法会在数据发生变化的时候你,只要引用了此里面分方法,方法就 ...
- 详解Vue中的computed和watch
作者:小土豆 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.cn/user/2436173500265335 1. 前言 作为一名Vue ...
- Vue核心知识-computed和watch的使用场景和方法
https://www.jianshu.com/p/bb7a2244c7ca
- vue中的computed(计算属性)和watch(监听属性)的特点,以及深度监听
//计算属性是根据data中已有的属性,计算得到一个新的属性, <div>全名:{{fullName}}</div> 创建计算属性通过computed关键字,它是一个对象 计算 ...
随机推荐
- Android Studio 停靠模式(Docked Mode)
如果之前选了任务一种模式,先全都取消了 然后点击Window -->Active Tool Window-->这个时候就可以选择Docked Mode了
- 虚函数重载(overwrite) 继承覆盖问题
引言 类接口需要添加默认参数,以适应不同情况调用, 但是clang-tidy 不允许在接口上设置默认参数,ps: 可能担心继承类里接口重新设置新默认参数而导致误用的情况 #include <st ...
- Python3 Windows服务器简单实现 手机访问
设备 1. PC with Win10 2. Python 3.5.1 步骤 打开CMD,cd 到e盘,mkdir pythonWebserver 建立文件夹 //在E盘建立文件夹,作为服务器的根目录 ...
- Python—构造单向链表数据类型
# _*_ coding=utf-8 _*_ class Node: """ 创建链表的属性 """ def __init__(self, ...
- ZJNU 1217 - 航线问题——高级
将所有航线的其中一边排序后,另一边进行类dp 定义一个数组c,c[i]表示在所有能够开通i条航线的组合中,位置序号最大的那条航线的序号的最小值 比如下面一个样例 1 3 2 4 3 1 4 2 此时对 ...
- Mongodb数据库(linux)——基础操作
简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.它是非关系型数据库,但其结构与MySQL又很相似,mysql中的表格,在这里被称为集合,mysql表格内的数据是一 ...
- JVM学习思维导图
- 为什么使用 document.write 需要将</script>拆分开
福州SEO:细心点的朋友可能会注意到,有些网站使用document.write动态加载JS的时候需要把</script>拆分开来写?如下面的例子所示: <script type='t ...
- opencv---颜色空间转化并实现物体跟踪
一.图像处理的基本操作 因为这是第一篇写opencv的笔记,故先讲讲在python下写opencv的基本操作.总共总结了三点如下: 开头一定要加编码声明:-*- coding: utf-8 -*- p ...
- 可用的 .net core 支持 RSA 私钥加密工具类
首先说明 MS并不建议私钥加密,而且.net 于安全的考虑,RSACryptoServiceProvider类解密时只有同时拥有公钥和私钥才可以,原因是公钥是公开的,会被多人持有,这样的数据传输是不安 ...