加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Vue.js第三天学习笔记(计算属性computed)

发布时间:2020-12-17 03:07:30 所属栏目:百科 来源:网络整理
导读:今天给大家分享下vue.js中的计算属性(computed) 示例一 computed的get属性 html: {{fullName}} js: 示例二 computed的get和set属性: html: {{b}} js: 本文已被整理到了《》,欢迎大家学习阅读。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大

今天给大家分享下vue.js中的计算属性(computed)

示例一

computed的get属性

html:

{{fullName}}

js:

export default { components: {

},ready: function() {

},methods: {
},data() {
return {
firstName: 'Foo',lastName: 'Bar'
}
},computed: {
fullName: {
// getter
get: function() {
return this.firstName + ' and ' + this.lastName
},// setter
set: function(newValue) {
var names = newValue.split(' and ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
}

示例二

computed的get和set属性:

html:

{{b}}

js:

export default { components: { },ready: function() { },methods: { updateData:function(){ this.b=this.b;//给 b 重新赋值时就会调用 b 的 set 属性,从而改变 c 的值 } },data() { return { a:'1:30',c:'' } },computed: { b:{ get: function() {//通过a的值改变b的值 var time=this.a; time = time ? time.split(':') : ''; let hours = time[0]; let minutes = time[time.length - 1]; return parseInt(hours) * 60 + parseInt(minutes); },set:function(newValue){//通过b值的改变,设置 c 的值 let newTimes = newValue; let hoursTime = parseInt(newTimes) / 60; let minutesTime = parseInt(newTimes) % 60; newTimes = newTimes + ''; hoursTime = hoursTime + ''; hoursTime = hoursTime ? hoursTime.split('.') : ''; this.c = hoursTime[0] + ':' + minutesTime; console.log(hoursTime[0] + ':' + minutesTime); } } } }

本文已被整理到了《》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读