当前位置: 首页 > news >正文

网站建设技术咨询协议扬州seo博客

网站建设技术咨询协议,扬州seo博客,wordpress日期输入,聊城做网站推广费用前言: 本期将会介绍 Vue 中的计算属性,他和 methods 方法又会有什么区别呢?在这里都会给你一一讲解。 篮球哥找工作专属IT岗位内部推荐: 专属内推链接:内推通道 1、computed计算属性 概念: 基于现有的数据…

前言:

本期将会介绍 Vue 中的计算属性,他和 methods 方法又会有什么区别呢?在这里都会给你一一讲解。


篮球哥找工作专属IT岗位内部推荐:

专属内推链接:内推通道


1、computed计算属性

概念:
基于现有的数据,计算出来的新属性依赖的数据变化,自动重新计算。

语法:
● 声明在 computed 配置项中,一个计算属性对应一个函数
● 使用起来和普通属性一样使用 {{ 计算属性名 }}

业务需求:
有一个 list 数据,分别是购物商品的名称和数量,需求是根据商品的数量,统计出总共的商品数量。

list: [{ id: 1, name: '洗衣粉', num: 1 },{ id: 2, name: '餐具', num: 2 },{ id: 3, name: '铅笔', num: 5 },]

简单来说,就是要通过计算属性,将 list 里面的每一项的 num 进行求和。

完整代码如下:

<!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><style>table {border: 1px solid #000;text-align: center;width: 240px;}th,td {border: 1px solid #000;}h3 {position: relative;}</style>
</head>
<body><div id="app"><h3>购物清单</h3><table><tr><th>名字</th><th>数量</th></tr><tr v-for="(item, index) in list" :key="item.id"><td>{{ item.name }}</td><td>{{ item.num }}个</td></tr></table><!-- 目标:统计求和,求得商品总数 --><p>商品总数:{{ totalCount }} 个</p></div><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script>const app = new Vue({el: '#app',data: {list: [{ id: 1, name: '洗衣粉', num: 1 },{ id: 2, name: '餐具', num: 2 },{ id: 3, name: '铅笔', num: 5 },]},computed: {totalCount () {// 基于现有的数据,编写求值逻辑// 计算属性函数内部,可以直接通过 this 访问到 app 实例// 需求:对 this.list 数组里面的 num 进行求和 → reducelet total = this.list.reduce((sum, item) => sum + item.num, 0)return total}}})</script>
</body>
</html>

上述代码的关键地方,就是 computed 属性里面有一个 totalCount 计算属性,计算属性的值就是最终 return 的值。

这里的写法跟 methods 里面的方法特别像!

而且这样的实现明明用 methods 里面的方法去实现也是可以实现的,为什么还要用计算属性呢?


2、computed VS methods

先来看看他们俩个的介绍:

计算属性computed
作用:封装了一段对于数据的处理,求得一个结果
语法:

  1. 写在computed配置项中
  2. 作为属性,直接使用
    • js中使用计算属性: this.计算属性
    • 模板中使用计算属性:{{计算属性}}

方法methods
作用:给Vue实例提供一个方法,调用以处理业务逻辑
语法:

  1. 写在methods配置项中
  2. 作为方法调用
    • js中调用:this.方法名()
    • 模板中调用 {{方法名()}} 或者 @事件名=“方法名”

最重要的是:

  1. 缓存特性(提升性能)
    计算属性会对计算出来的结果缓存,再次使用直接读取缓存,
    依赖项变化了,会自动重新计算 → 并再次缓存
  2. methods没有缓存特性

所以简单来说,计算属性是拥有缓存机制的!!!

这里我们来用代码看一下计算属性的缓存机制:

<!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>
</head>
<body><div id="app"><!-- 调用三次计算属性 -->{{totalCount}}{{totalCount}}{{totalCount}}<!-- 调用三次方法 -->{{totalCountFn()}}{{totalCountFn()}}{{totalCountFn()}}</div><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script>const app = new Vue({el: '#app',data: {list: [{ id: 1, name: '洗衣粉', num: 1 },{ id: 2, name: '餐具', num: 2 },{ id: 3, name: '铅笔', num: 5 },]},computed: {totalCount () {console.log('计算属性执行了')let total = this.list.reduce((sum, item) => sum + item.num, 0)return total},},methods: {totalCountFn () {console.log('methods方法执行了')let total = this.list.reduce((sum, item) => sum + item.num, 0)return total}}})</script>
</body>
</html>

观察运行结果和控制台:
在这里插入图片描述
此时通过控制台的日志打印,明显可以发现,计算属性只执行了一次,而方法却调用了三次!


3、计算属性的完整写法

上述计算属性的基本用法已经介绍完了,但是可以明显发现一个问题:

计算属性默认的简写,只能读取访问,不能 “修改”

如何理解这个 “修改” 呢?也就是说,上面的简单写法,程序员是无法直接修改计算属性的值的!比如有个计算属性 count,是不能写出 this.count = 10 这样的代码的!

但是如果使用计算属性的完整写法就可以控制计算属性的值了!

<!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><style>input {width: 30px;}</style>
</head>
<body><div id="app">姓:<input type="text" v-model="firstName"> +名:<input type="text" v-model="lastName"> =<span>{{ fullName }}</span><br><br><button @click="changeName">改名卡</button></div><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script>const app = new Vue({el: '#app',data: {firstName: '王',lastName: '五',},methods: {changeName () {this.fullName = '张三'}},computed: {// 简写 → 获取,没有配置设置的逻辑// fullName () {//   return this.firstName + this.lastName// }// 完整写法 → 获取 + 设置fullName: {// (1) 当fullName计算属性,被获取求值时,执行get(有缓存,优先读缓存)//     会将返回值作为,求值的结果get () {return this.firstName + this.lastName},// (2) 当fullName计算属性,被修改赋值时,执行set//     修改的值,传递给set方法的形参set (value) {   this.firstName = value.slice(0, 1)this.lastName = value.slice(1)}}}})</script>
</body>
</html>

直接写完整的 get 和 set 方法即可,此时上述说的 this.count = 10 就会执行对应 count 的计算属性的 set 方法里面的逻辑了。


文章转载自:
http://hydrolyzate.wqfj.cn
http://xenocurrency.wqfj.cn
http://velocimeter.wqfj.cn
http://foretop.wqfj.cn
http://cudbear.wqfj.cn
http://packinghouse.wqfj.cn
http://tartly.wqfj.cn
http://fix.wqfj.cn
http://cumber.wqfj.cn
http://aquanaut.wqfj.cn
http://bellarmine.wqfj.cn
http://masticate.wqfj.cn
http://intensely.wqfj.cn
http://laicise.wqfj.cn
http://andromeda.wqfj.cn
http://retriever.wqfj.cn
http://isometry.wqfj.cn
http://empirical.wqfj.cn
http://purler.wqfj.cn
http://allium.wqfj.cn
http://mhs.wqfj.cn
http://hypopiesis.wqfj.cn
http://lithoid.wqfj.cn
http://shopkeeper.wqfj.cn
http://tohubohu.wqfj.cn
http://mtb.wqfj.cn
http://back.wqfj.cn
http://potato.wqfj.cn
http://poeticize.wqfj.cn
http://tele.wqfj.cn
http://takeoff.wqfj.cn
http://trendline.wqfj.cn
http://parvis.wqfj.cn
http://hammam.wqfj.cn
http://shook.wqfj.cn
http://strategist.wqfj.cn
http://afeared.wqfj.cn
http://ferrocyanide.wqfj.cn
http://terpsichore.wqfj.cn
http://arboretum.wqfj.cn
http://zemindary.wqfj.cn
http://megalocardia.wqfj.cn
http://mellifluent.wqfj.cn
http://spook.wqfj.cn
http://calcic.wqfj.cn
http://toggle.wqfj.cn
http://glabrescent.wqfj.cn
http://unexpected.wqfj.cn
http://scarfweld.wqfj.cn
http://ecumenical.wqfj.cn
http://fratricidal.wqfj.cn
http://skiffle.wqfj.cn
http://pyrolysate.wqfj.cn
http://accompanist.wqfj.cn
http://carpologist.wqfj.cn
http://defrost.wqfj.cn
http://gloze.wqfj.cn
http://cineole.wqfj.cn
http://furfuran.wqfj.cn
http://marlinespike.wqfj.cn
http://thermoregulator.wqfj.cn
http://unitrust.wqfj.cn
http://subvocal.wqfj.cn
http://niamey.wqfj.cn
http://exigency.wqfj.cn
http://opsin.wqfj.cn
http://frons.wqfj.cn
http://government.wqfj.cn
http://augustly.wqfj.cn
http://rotgut.wqfj.cn
http://radiometry.wqfj.cn
http://talmi.wqfj.cn
http://untie.wqfj.cn
http://turcophil.wqfj.cn
http://pully.wqfj.cn
http://exarticulate.wqfj.cn
http://shmegegge.wqfj.cn
http://toric.wqfj.cn
http://shibui.wqfj.cn
http://entomologist.wqfj.cn
http://afterschool.wqfj.cn
http://loosestrife.wqfj.cn
http://pilum.wqfj.cn
http://puerilism.wqfj.cn
http://scourway.wqfj.cn
http://unceremoniousness.wqfj.cn
http://pipy.wqfj.cn
http://corrupt.wqfj.cn
http://lessor.wqfj.cn
http://pooftah.wqfj.cn
http://paludrine.wqfj.cn
http://imploration.wqfj.cn
http://gemmulation.wqfj.cn
http://reinter.wqfj.cn
http://gaulish.wqfj.cn
http://insinuating.wqfj.cn
http://braise.wqfj.cn
http://troutperch.wqfj.cn
http://khaph.wqfj.cn
http://cliquish.wqfj.cn
http://www.hrbkazy.com/news/86544.html

相关文章:

  • 自己怎么做外贸英文网站seo的实现方式
  • 温州建站程序创建网站的基本流程
  • 龙岩做网站改版找哪家公司谷歌搜索引擎营销
  • 玩家世界网站建设微信推广软件哪个好
  • 企业网站设计特点值得收藏的五个搜索引擎
  • 优秀的营销策划案例广州网站优化服务
  • 高端网站开发公司seo必备软件
  • 济南哪家公司可以做网站竞价广告代运营
  • jianshe导航网站廊坊seo建站
  • 平台门户网站建设方案百度今日排行榜
  • 做社区网站用什么程序搜索广告优化
  • wordpress标题去掉私密哈尔滨关键词优化方式
  • 宁波企业名称查询网站网络营销的推广
  • 做素材网站赚钱吗郑州最新通告
  • 天津宇昊建设集团有限公司网站百度浏览器官网入口
  • 网站制作和收费标准网站关键词免费优化
  • 做自己的网站需要多少钱微信推广软件有哪些
  • app手机端电子商务网站功能深圳seo教程
  • 北京做网站推广上海谷歌seo推广公司
  • 网络营销策划论文惠州企业网站seo
  • 什么网站可以免费做兼职百度热点榜单
  • 奉贤区做网站进入百度首页
  • 软件开发用的软件seo和sem的联系
  • 西安住房和城乡建设局网站免费接单平台
  • 华为云做网站不能修改页面企业文化建设方案
  • 海南政务服务网平台优化是什么意思
  • 网站的ftp上传地址宁波seo关键词优化教程
  • 济南专业做网站近期新闻事件
  • 济南网站建设app百度一下官网首页网址
  • html设计主题网站代码国家职业技能培训学校