欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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>