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

computed 计算后返回新 一个数据受多个数据影响(比如:计算总价格、过滤某些数据)
computed是用来处理你使用watch和methods的时候无法处理(比如有缓存的时候监听不了数据变化),或者是处理起来并不太恰当的情况的,利用computed处理methods存在的重复计算情况
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的使用场景的更多相关文章

  1. Vue.js中 computed 和 methods 的区别

    官方文档中已经有对其的解释了,在这里把我的理解记录一下Vue中的methods.watch.computed computed 的使用场景 HTML模板中的复杂逻辑表达式,为了防止逻辑过重导致不易维护 ...

  2. vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)

    _ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...

  3. 实例分析Vue.js中 computed和methods不同机制

    在vue.js中,有methods和computed两种方式来动态当作方法来用的 1.首先最明显的不同 就是调用的时候,methods要加上() 2.我们可以使用 methods 来替代 comput ...

  4. vue Watcher分类 computed watch

    1.Watcher构造函数源码部分代码 if (options) { this.deep = !!options.deep this.user = !!options.user this.lazy = ...

  5. Vue 中的 computed 和 methods

    Vue 中的 computed 和 methods 使用 computed 性能会更好. 如果你不希望缓存,可以使用 methods 属性.

  6. vue中methods,computed,filters,watch的总结

    08.28自我总结 vue中methods,computed,filters,watch的总结 一.methods methods属性里面的方法会在数据发生变化的时候你,只要引用了此里面分方法,方法就 ...

  7. 详解Vue中的computed和watch

    作者:小土豆 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.cn/user/2436173500265335 1. 前言 作为一名Vue ...

  8. Vue核心知识-computed和watch的使用场景和方法

    https://www.jianshu.com/p/bb7a2244c7ca

  9. vue中的computed(计算属性)和watch(监听属性)的特点,以及深度监听

    //计算属性是根据data中已有的属性,计算得到一个新的属性, <div>全名:{{fullName}}</div> 创建计算属性通过computed关键字,它是一个对象 计算 ...

随机推荐

  1. python pandas 对带时间序列的数据进行重采样处理

    今天老板要处理一批带有时间序列的数据,源数据为1秒钟一行的csv数据,处理之后变成15分钟一行的数据. 源数据示例如下: time B00 B01 ... RollMean2.5 RollMean10 ...

  2. 如何判断Office是32位还是64位?

    对于持续学习VBA的老铁们,有必要了解Office的位数. 如果系统是32位的,则不需要判断Office位数了,因为只能安装32位Office. 下面只讨论64位系统中,Office的位数判断问题. ...

  3. BBS数据库设计

    BBS数据库设计 一.BBS数据库设计 # models.py from django.db import models # Create your models here. from django. ...

  4. 四十二、LAMP与LNMP web架构深度优化实战-第一部

    1.nginx.conf配置文件基本参数优化 1.1 隐藏nginx header内版本号信息 一些特定的系统及服务漏洞一般都和特定的软件版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称及版本等信 ...

  5. ubuntu Elasticsearch环境搭建

    https://www.cnblogs.com/pigzhu/p/4705870.html

  6. Codeforces Educational Round 81 解题报告

    前置扯淡 赛前:这场\(Div2\)呀,那我写\(3\)题就行,\(D\)题尽力就好 赛中:啊啊,\(ABC\)我全过了\(pretest\),我太强了!!这把上蓝稳了 赛后:\(woc\),为啥被\ ...

  7. ansible批量部署(一)

    自动化运维工具shell脚本/Ansible(无客户端)/Saltstack(master-minion) 回顾服务器部署的流程:买云主机->环境部署->软件部署->配置部署-> ...

  8. 爬虫基本库request使用—爬取猫眼电影信息

    使用request库和正则表达式爬取猫眼电影信息. 1.爬取目标 猫眼电影TOP100的电影名称,时间,评分,等信息,将结果以文件存储. 2.准备工作 安装request库. 3.代码实现 impor ...

  9. Unittest - Python 使用总结

    Unittest - Python 使用总结 批量执行 一.UnitTest TestSuite 控制用例执行的顺序 UnitTest 框架默认 main() 方法根据 ASCII 码的顺序加载测试用 ...

  10. 使用js/jquery动态提交表单数据到搜索引擎或者接口

    现在一般需要用jquery等方式动态提交到某个接口,比如通过iframe <iframe id="mainIframe" name="mainIframe" ...