1. 程式人生 > 實用技巧 >vue原始碼分析(三)>>:computed

vue原始碼分析(三)>>:computed

看完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:觸發邏輯