计算属性的重点突出在属性两个字上(属性是名词),首先它是个 属性其次这个属性有计算的能力(计算是动词),这里的 计算就是个函数;简单点说,它就是一个能够将计算结果缓存起来的属性(将行为转化成了静态的属性),仅此而已;可以想象为缓存!
<!DOCTYPE html> <html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <!--调用方法用:方法名()--> <div>currentTime1:{{currentTime1()}}</div> <div>currentTime2:{{currentTime2}}</div> </div> <!--1.导入vue.js--> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script> <script> var vm = new Vue({ el: "#app", data: { message: "hello,vue!" }, methods: { currentTime1: function () { return Date.now(); // 返回一个时间戳 } }, // 计算属性 注意:methodds和computed中的方法名不能重名 computed: { currentTime2: function () { this.message; return Date.now(); // 返回一个时间戳 } } }); </script> </body> </html>
注意:methods 和 computed 里的东西不能重名
说明:
结论:
调用方法时,每次都需要进行计算,既然有计算过程则必定产生系统开销,那如果这个结果是不经常变化的呢?此时就可以考虑将这个结果缓存起来,采用计算属性可以很方便的做到这一点,计算属性的主要特性就是为了将不经常变化的计算结果进行缓存,以节约我们的系统开销;
在 Vue.js中我们使用
比如准备制作一个待办事项组件(todo),该组件由待办标题(todo-title)和待办内容(todo-items)组成,但这三个组件又是相互独立的,该如何操作呢?
<div id="vue"> <todo></todo> </div> <script type="text/javascript"> Vue.component('todo', { template: '<div>\ <div>待办事项</div>\ <ul>\ <li>学习Vue</li>\ </ul>\ </div>' }); </script>
1- 将上面的代码留出一个插槽,即 slot
Vue.component('todo', { template: '<div>\ <slot name="todo-title"></slot>\ <ul>\ <slot name="todo-items"></slot>\ </ul>\ </div>' });
2- 定义一个名为 todo-title 的待办标题组件 和 todo-items 的待办内容组件
Vue.component('todo-title', { props: ['title'], template: '<div>{{title}}</div>' }); //这里的index,就是数组的下标,使用for循环遍历的时候,可以循环出来! Vue.component('todo-items', { props: ['item', 'index'], template: '<li>{{index + 1}}. {{item}}</li>' });
3- 实例化 Vue 并初始化数据
var vm = new Vue({ el: '#vue', data: { todoItems: ['Java', '运维', '前端'] } });
4- 将这些值,通过插槽插入
<div id="vue"> <todo> <todo-title slot="todo-title" title="vue好学"></todo-title> <todo-items slot="todo-items" v-for="(item, index) in todoItems" v-bind:item="item" v-bind:index="index" :key="index"></todo-items> </todo> </div>
说明:我们的 todo-title 和 todo-items 组件分别被分发到了 todo 组件的 todo-title 和 todo-items 插槽中
<!DOCTYPE html> <html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <todo> <!--:title 是 v-bind:title的简写--> <todo-title slot="todo-title" :title="title"></todo-title> <todo-items slot="todo-items" v-for="item in items" :item="item"></todo-items> </todo> </div> <!--1.导入vue.js--> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script> <script> // slot:插槽 Vue.component("todo",{ template: '<div>\ <slot name="todo-title"></slot>\ <ul>\ <slot name="todo-items"></slot>\ </ul>\ </div>' }); Vue.component("todo-title",{ props: ['title'], template: '<div>{{title}}</div>' }); Vue.component("todo-items",{ props: ['item'], template: '<li>{{item}}</li>' }); var vm = new Vue({ el: "#app", data: { title: "书架", items: ["Java","SQL","Linux"] } }); </script> </body> </html>
通过以上代码不难发现,数据项在 Vue 的实例中,但删除操作要在组件中完成,那么组件如何才能删除 Vue 实例中的数据呢?此时就涉及到参数传递与事件分发了,Vue 为我们提供了自定义事件的功能很好的帮助我们解决了这个问题;使用 this.$emit(‘自定义事件名’, 参数),操作过程如下:
var vm = new Vue({ el: '#vue', data: { title: "vue好学", todoItems: ['Java', '运维', '前端'] }, methods: { // 该方法可以被模板中自定义事件触发 removeTodoItems: function (index) { console.log("删除 " + this.todoItems[index] + " 成功"); // splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目,其中 index 为添加/删除项目的位置,1 表示删除的数量 this.todoItems.splice(index, 1); } } });
Vue.component('todo-items', { props: ['item', 'index'], template: '<li>{{index + 1}}. {{item}} <button @click="remove_component">删除</button></li>', methods: { remove_component: function (index) { // 这里的 remove 是自定义事件的名称,需要在 HTML 中使用 v-on:remove 的方式指派 this.$emit('remove', index); } } });
<!--增加了 v-on:remove="removeTodoItems(index)" 自定义事件,该事件会调用 Vue 实例中定义的名为 removeTodoItems 的方法--> <todo-items slot="todo-items" v-for="(item, index) in todoItems" v-bind:item="item" v-bind:index="index" :key="index" v-on:remove="removeTodoItems(index)"></todo-items>
<!DOCTYPE html> <html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <todo> <!--:title 是 v-bind:title 的简写--> <todo-title slot="todo-title" :title="title"></todo-title> <todo-items slot="todo-items" v-for="(item,index) in items" :item="item" :index="index" :key="index" @remove="removeItems(index)"></todo-items> </todo> </div> <!--1.导入vue.js--> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script> <script> // slot:插槽 Vue.component("todo",{ template: '<div>\ <slot name="todo-title"></slot>\ <ul>\ <slot name="todo-items"></slot>\ </ul>\ </div>' }); Vue.component("todo-title",{ props: ['title'], template: '<div>{{title}}</div>' }); Vue.component("todo-items",{ props: ['item','index'], // @clice 是 v-on:click 的简写 template: '<li>{{index}}----{{item}} <button @click="remove">删除</button></li>', methods: { remove: function (index) { // this.$emit 自定义事件分发 this.$emit('remove',index); } } }); var vm = new Vue({ el: "#app", data: { title: "书架", items: ["Java","SQL","Linux"] }, methods: { removeItems: function (index) { console.log("删除了" + this.items[index]); // 删除下标开始的一个元素 this.items.splice(index,1); } } }); </script> </body> </html>