<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="../vue.js"></script>
<div id="#app">
<!--直接拼接,语法过于繁琐-->
<h2>{{fir}} {{last}}</h2>
<!--通过methods-->
<h2>{{getFullName}}</h2>
<!-- 通过computed-->
<h2>{{fullName}}</h2>
</div>
<script>
const app=new Vue({
el:'#app',
data:{
fir:'Kobe',
last:'Bryant',
},
computed:{
fullName: function () {
//性能高,多次调用,因为会缓存,比较若没有改变,只会调用一次
console.log('fullname')
return this.fir+' '+this.last
}
},
methods:{
getFullName:function () {
//性能低,多次使用调用多次,哪怕不变
console.log('get')
return this.fir+' '+this.last
},
}
})
</script>
</body>
</html>