Vue计算属性是什么

发布时间:2021-07-15 14:51:05 作者:小新
来源:亿速云 阅读:123

这篇文章主要为大家展示了“Vue计算属性是什么”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Vue计算属性是什么”这篇文章吧。

具体内容如下

①模板内的表达式实际上只用于简单的运算,对于复杂逻辑,使用计算机属性。

②基础例子:

<div id = "example"> 
 <p>Original message:"{{message}}"</p> 
 <p>Computed reversed message:"{{reversedMessage}}"</p> 
</div>
var vm = new Vue({ 
 el:"#example", 
 data:{ 
 message:"Hello" 
 }, 
 computed:{ 
 //a computed getter 
 reversedMessage:function(){ 
  //'this' points to the vm instance 
  return this.message.split('').reverse().join('') 
 } 
 } 
})

这里我们声明了一个计算机属性reversedMessage,我们提供的函数将用作属性vm.reversedMessage的getter。

③计算机缓存 vs Methods

可以通过调用表达式中的method来达到同样的效果:

<p>Reversed message:"{{reversedMessage}}"</p>
//in component 
methods:{ 
 reversedMessage:function(){ 
 return this.message.split('').reverse()/join('') 
 } 
}

可以将同一个函数定义为一个method而不是一个计算机属性。对于最终的结果,两种方式确实是相同的。然而不同的计算机属性是基于它们的依赖进行缓存的。计算属性只有在它的相关依赖发生改变时才会重新求值,这就意味着只要message还没有改变,多次访问reversedMessage计算属性会立即返回之前的计算结果,而不必再次执行函数。
下面的计算属性将不再更新,因为Date.now()不是响应式依赖:

computed:{ 
 now:function(){ 
 return Date.now() 
 } 
}

只要发生重新渲染,method调用总会执行该函数。

④computed属性 vs watch属性

<div id= "demo">{{fullName}}</div>

watch:

var vm = new Vue({ 
 el:"#demo", 
 data:{ 
 firstName:"Foo", 
 lastName:"Bar", 
 fullName:"Foo Bar" 
 }, 
 watch:{ 
 firstName:function(val){ 
  this.fullName = val + '' + this.lastName 
 }, 
 lastName:function(val){ 
 this.fullName = this.firstName + '' +val 
 } 
 } 
})

computed:

var vm = new Vue({ 
 el:'#demo', 
 data:{ 
 firstName:'Foo', 
 lastName:'Bar' 
 }, 
 computed:{ 
 fullName:function(){ 
  return this.firstName + ' ' + this.lastName 
 } 
 } 
})

⑤计算setter:

计算属性默认只有getter,不过在需要是可以提供一个setter:

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

在运行vm.fullName = 'John Doe'时,setter会被调用,vm.firstName和vm.lastName  也相应的会被更新。

⑥观察watchers

    当想要在数据变化相应时,执行异步操作或开销较大的操作,这是很有用的。

<div id="watch-example"> 
 <p> 
 Ask a yes/no question: 
 <input v-model="question"> 
 </p> 
 <p>{{ answer }}</p> 
</div>
<script src="https://unpkg.com/axios@0.12.0/dist/axios.min.js"></script> 
<script src="https://unpkg.com/lodash@4.13.1/lodash.min.js"></script> 
<script> 
var watchExampleVM = new Vue({ 
 el: '#watch-example', 
 data: { 
 question: '', 
 answer: 'I cannot give you an answer until you ask a question!' 
 }, 
 watch: { 
 // 如果 question 发生改变,这个函数就会运行 
 question: function (newQuestion) { 
  this.answer = 'Waiting for you to stop typing...' 
  this.getAnswer() 
 } 
 }, 
 methods: { 
 // _.debounce 是一个通过 lodash 限制操作频率的函数。 
 // 在这个例子中,我们希望限制访问yesno.wtf/api的频率 
 // ajax请求直到用户输入完毕才会发出 
 // 学习更多关于 _.debounce function (and its cousin 
 // _.throttle), 参考: https://lodash.com/docs#debounce 
 getAnswer: _.debounce( 
  function () { 
  var vm = this 
  if (this.question.indexOf('?') === -1) { 
   vm.answer = 'Questions usually contain a question mark. ;-)' 
   return 
  } 
  vm.answer = 'Thinking...' 
  axios.get('https://yesno.wtf/api') 
   .then(function (response) { 
   vm.answer = _.capitalize(response.data.answer) 
   }) 
   .catch(function (error) { 
   vm.answer = 'Error! Could not reach the API. ' + error 
   }) 
  }, 
  // 这是我们为用户停止输入等待的毫秒数 
  500 
 ) 
 } 
}) 
</script>

 在这个示例中,使用watch选项允许我们执行异步操作,限制我们执行该操作的频率,并在得到最终结果前,设置中间状态,这是计算属性无法做到的。

以上是“Vue计算属性是什么”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Vue.js计算属性
  2. vue.js计算属性指的是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue

上一篇:C#读取Excel及显示和存储的实现方法

下一篇:在SQL语句统计数据时sum和count函数中使用if判断条件的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》