vue计算属性

 

 

vue计算属性

vue计算属性

newValue=app.fullName的值

 

计算属性的缓存

 

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<script src="vue-2.5.21/dist/vue.js" type="text/javascript" charset="utf-8"></script>
	<style>
	</style>
</head>

<body>
	<div id="app">
		<!-- 1. 直接拼接 语法过于繁琐 -->
		<h2>{{firstName}} {{lastName}}</h2>
		<!-- 2. 通过定义methods -->
		<h2>{{getFullName()}}</h2>
		<!-- 3. 通过computed -->
		<h2>{{fullName}}</h2>

	</div>

</body>
<script type="text/javascript">
	const app = new Vue({
		el: "#app",
		data: {
			firstName: "Lebron",
			lastName: "James",
		},
		computed: {
			// 不用 get
			fullName: {
				// set不常用
				set: function(newValue){
					console.log(newValue);
					const names = newValue.split(" ");
					this.firstName = names[0];
					this.lastName = names[1];
				},
				get: function(){
					return this.firstName+" "+this.lastName;
				}
			}
			
		},
		methods: {
			getFullName: function(){
				return this.firstName+" "+this.lastName;
			}
		}
	});
</script>

</html>

 

 

上一篇:jQuery笔记


下一篇:vue .sync修饰符使用详解