2_3 Vue 计算属性,方法监听
程序员文章站
2022-05-17 20:04:36
...
Vue 计算属性,方法监听
<!DOCTYPE html>
<html>
<head>
<title>计算属性</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
{{fullName}}
</div>
<script type="text/javascript">
var app = new Vue({
el:'#app',
data:{
firstName:'zhang',
lastName:'xue'
},
//计算属性:有缓存机制
computed:{
fullName:function(){
return this.firstName + ' ' + this.lastName;
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>方法</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
{{fullName()}}
</div>
<script type="text/javascript">
var app = new Vue({
el:'#app',
data:{
firstName:'zhang',
lastName:'xue'
},
//方法:无缓存机制,性能不佳
methods:{
fullName:function(){
return this.firstName+' '+this.lastName;
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>监听</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
{{fullName}}
</div>
<script type="text/javascript">
var app = new Vue({
el:'#app',
data:{
firstName:'zhang',
lastName:'xue',
fullName:'zhnag xue'
},
//监听
watch:{
firstName:function(){
this.fullName=this.firstName+ ' ' + this.lastName;
},
lastName:function(){
this.lastName=this.firstName+ ' '+ this.lastName;
}
}
})
</script>
</body>
</html>
上一篇: 操作vue项目
下一篇: Play 2.6 OAuth的使用