最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Vue中computed与methods使用区别

来源:动视网 责编:小采 时间:2020-11-27 19:50:39
文档

Vue中computed与methods使用区别

Vue中computed与methods使用区别:这次给大家带来Vue中computed与methods使用区别,Vue中computed与methods使用的注意事项有哪些,下面就是实战案例,一起来看一下。Vue中computed可以用来简单的拼接需要展示的数据computed and methods拼接展示数据的任务, 也可以用methods完
推荐度:
导读Vue中computed与methods使用区别:这次给大家带来Vue中computed与methods使用区别,Vue中computed与methods使用的注意事项有哪些,下面就是实战案例,一起来看一下。Vue中computed可以用来简单的拼接需要展示的数据computed and methods拼接展示数据的任务, 也可以用methods完
 这次给大家带来Vue中computed与methods使用区别,Vue中computed与methods使用的注意事项有哪些,下面就是实战案例,一起来看一下。

Vue中computed可以用来简单的拼接需要展示的数据

computed and methods

拼接展示数据的任务, 也可以用methods完成, 但当页面的数据变化时, methods中的方法会被重新调用(产生不必要的性能消耗), 而methods内的方法只有和自身有关的数据变化时才会被调用

一个简单的实例

computed只在初始化时被调用

computed只在初始化时被调用

methods会在数据变化时被调用, 即使变动的数据与自身无关

测试源码

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>computed的使用</title>
 <script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>
<body>
 <p id="root">
 </p>
 <script>
 var vm = new Vue({
 el: "#root",
 data: {
 name: "zhaozhao",
 age: 13,
 hobby: 'Python',
 nameAgeStyle: {
 fontSize: "20px",
 color: "#0c8ac5"
 }
 },
 template: `<p>
 <p v-bind:style="nameAgeStyle">computed方式渲染: {{nameAndAge}}</p>
 <p v-bind:style="nameAgeStyle">methods 方式渲染: {{getNameAndAge()}}</p>
 <br>
 <input type="text" v-model="hobby">
 <p>爱好: {{hobby}}</p>
 <p>{{noUse()}}</p>
 </p>`,
 computed: {
 nameAndAge: {
 get(){
 console.log('调用computed');
 return `${this.name} ==> ${this.age}`;
 }
 }
 },
 methods: {
 getNameAndAge() {
 console.log('调用methods');
 return `${this.name} ==> ${this.age}`;
 },
 noUse(){
 console.log("=methods==nouse==");
 }
 }
 })
 </script>
</body>
</html>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

jQuery+Ajax验证用户名步骤详解

v-show添加表达式步骤详解

文档

Vue中computed与methods使用区别

Vue中computed与methods使用区别:这次给大家带来Vue中computed与methods使用区别,Vue中computed与methods使用的注意事项有哪些,下面就是实战案例,一起来看一下。Vue中computed可以用来简单的拼接需要展示的数据computed and methods拼接展示数据的任务, 也可以用methods完
推荐度:
标签: 区别 VUE 不同
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top