🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] >[success] # computed方法生成计算属性 下面讲解一下 **composition API** 的新版 **computed计算属性** 如何使用。 >[success] ## computed 普通用法 1. **Vue2.x 版本 computed 使用方法** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed方法生成计算属性</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // vue2.x 版本 computed 计算属性 const app = Vue.createApp({ data(){ return{ cd: 'cd' } }, computed: { abc(){ return this.cd + 'efg' } }, template: ` <div> {{ abc }} </div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 2. **Vue 3 版本 computed 使用方法** 在 **composition API** 中使用 **computed 计算属性** ,只需要引入一个 **computed 方法** ,在使用时候返回一个计算后的值即可。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed方法生成计算属性</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // computed 计算属性 const app = Vue.createApp({ setup(){ const { ref, computed } = Vue const count = ref(0) // 点击事件 const handleClick = () => { count.value += 1 } // 计算属性 const countAddFive = computed(() => { return count.value + 5 }) return { count, countAddFive, handleClick } }, template: ` <div> <span @click="handleClick">{{ count }}</span> -- {{ countAddFive }} </div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 或者可以写的复杂一些,用 **get** 方法来写。 >[success] ## computed 的 get set方法 1. **get** **get** 实现的效果, **与上面的普通写法实现的效果是一样的** , **get** 表示获取返回的这个值。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed方法生成计算属性</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // computed 计算属性 const app = Vue.createApp({ setup(){ const { ref, computed } = Vue const count = ref(0) // 点击事件 const handleClick = () => { count.value += 1 } // 计算属性 const countAddFive = computed({ get: () => { return count.value + 5 } }) return { count, countAddFive, handleClick } }, template: ` <div> <span @click="handleClick">{{ count }}</span> -- {{ countAddFive }} </div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 2. **set** **当计算属性被修改时**,会触发 **set** 这个方法,如下: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed方法生成计算属性</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // computed 计算属性 const app = Vue.createApp({ setup(){ const { ref, computed } = Vue const count = ref(0) // 点击事件 const handleClick = () => { count.value += 1 } // 计算属性 let countAddFive = computed({ get: () => { return count.value + 5 }, set: () => { count.value = 10 } }) // 2s 后修改 countAddFive 计算属性 setTimeout(() => { countAddFive.value = 100 }, 3000) return { count, countAddFive, handleClick } }, template: ` <div> <span @click="handleClick">{{ count }}</span> -- {{ countAddFive }} </div> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## computed 的 get set使用实例 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed方法生成计算属性</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // computed 计算属性 const app = Vue.createApp({ setup(){ const { ref, computed } = Vue const count = ref(0) // 点击事件 const handleClick = () => { count.value += 1 } // 计算属性 let countAddFive = computed({ get: () => { return count.value + 5 }, set: (param) => { count.value = param - 5 } }) // 2s 后修改 countAddFive 计算属性 setTimeout(() => { countAddFive.value = 100 }, 3000) return { count, countAddFive, handleClick } }, template: ` <div> <span @click="handleClick">{{ count }}</span> -- {{ countAddFive }} </div> ` }) const vm = app.mount('#root') </script> </html> ~~~