vue原始碼分析(三)>>:computed
阿新 • • 發佈:2020-08-27
看完watch在看看computed時怎麼實現的:
step1:用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed</title> </head> <body> <div id="app"> <p>歡迎:{{userName}}</p> <p>歡迎:{{userNameM()}}</p> <button @click="handleClick">click</button> </div> <script src="../../dist/vue.js"></script> <script> let vm = new Vue({ el:'#app', data:{ user:{ firstName:'張', lastName:'三豐' }, }, methods: { handleClick(){ this.user.firstName = "周"; }, userNameM(){ return this.user.firstName + this.user.lastName; } }, computed: { userName(){ return this.user.firstName+ this.user.lastName; } }, }) </script> </body> </html>
用方法也能實現同樣的效果,先寫上。
step2:原始碼實現
step3:觸發邏輯