- VueJS Tutorial
- VueJS - Home
- VueJS - Overview
- VueJS - Environment Setup
- VueJS - Introduction
- VueJS - Instances
- VueJS - Template
- VueJS - Components
- VueJS - Computed Properties
- VueJS - Watch Property
- VueJS - Binding
- VueJS - Events
- VueJS - Rendering
- VueJS - Transition & Animation
- VueJS - Directives
- VueJS - Routing
- VueJS - Mixins
- VueJS - Render Function
- VueJS - Reactive Interface
- VueJS - Examples
- VueJS Useful Resources
- VueJS - Quick Guide
- VueJS - Useful Resources
- VueJS - Discussion
VueJS - 计算属性
我们已经看到了 Vue 实例和组件的方法。计算属性类似于方法,但与方法相比有一些区别,我们将在本章中讨论。
在本章结束时,我们将能够决定何时使用方法以及何时使用计算属性。
让我们通过一个例子来理解计算属性。
例子
<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "computed_props">
FirstName : <input type = "text" v-model = "firstname" /> <br/><br/>
LastName : <input type = "text" v-model = "lastname"/> <br/><br/>
<h1>My name is {{firstname}} {{lastname}}</h1>
<h1>Using computed method : {{getfullname}}</h1>
</div>
<script type = "text/javascript" src = "js/vue_computedprops.js"></script>
</body>
</html>
vue_computeprops.js
var vm = new Vue({
el: '#computed_props',
data: {
firstname :"",
lastname :"",
birthyear : ""
},
computed :{
getfullname : function(){
return this.firstname +" "+ this.lastname;
}
}
})
在这里,我们创建了包含名字和姓氏的.html文件。名字和姓氏是一个使用名字和姓氏属性绑定的文本框。
我们正在调用计算方法 getfullname,它返回输入的名字和姓氏。
computed :{
getfullname : function(){
return this.firstname +" "+ this.lastname;
}
}
当我们在文本框中键入内容时,当属性名或姓氏发生更改时,函数会返回相同的结果。因此,在计算的帮助下,我们不必做任何具体的事情,例如记住调用函数。通过计算,它会被自身调用,因为内部使用的属性发生变化,即名字和姓氏。
在下面的浏览器中也显示相同的内容。在文本框中输入内容,系统将使用计算函数进行更新。
现在,让我们尝试理解方法和计算属性之间的区别。两者都是对象。里面定义了函数,有返回值。
对于方法,我们将其称为函数,并将计算的称为属性。使用以下示例,让我们了解方法和计算属性之间的区别。
<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "computed_props">
<h1 style = "background-color:gray;">Random No from computed property: {{getrandomno}}</h1>
<h1>Random No from method: {{getrandomno1()}}</h1>
<h1>Random No from method : {{getrandomno1()}}</h1>
<h1 style = "background-color:gray;">Random No from computed property: {{getrandomno}}</h1>
<h1 style = "background-color:gray;">Random No from computed property: {{getrandomno}}</h1>
<h1 style = "background-color:gray;">Random No from computed
property: {{getrandomno}}</h1>
<h1>Random No from method: {{getrandomno1()}}</h1>
<h1>Random No from method: {{getrandomno1()}}</h1>
</div>
<script type = "text/javascript">
var vm = new Vue({
el: '#computed_props',
data: {
name : "helloworld"
},
methods: {
getrandomno1 : function() {
return Math.random();
}
},
computed :{
getrandomno : function(){
return Math.random();
}
}
});
</script>
</body>
</html>
在上面的代码中,我们创建了一个名为getrandomno1的方法和一个带有函数getrandomno 的计算属性。两者都使用 Math.random() 返回随机数。
它在浏览器中的显示如下图所示。该方法和计算属性被多次调用以显示差异。
如果我们查看上面的值,我们会发现从计算属性返回的随机数保持不变,无论调用多少次。这意味着每次调用它时,都会更新所有值的最后一个值。而对于方法来说,它是一个函数,因此每次调用它时都会返回不同的值。
在计算属性中获取/设置
在本节中,我们将通过示例了解计算属性中的 get/set 函数。
例子
<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "computed_props">
<input type = "text" v-model = "fullname" />
<h1>{{firstName}}</h1>
<h1>{{lastName}}</h1>
</div>
<script type = "text/javascript">
var vm = new Vue({
el: '#computed_props',
data: {
firstName : "Terry",
lastName : "Ben"
},
methods: {
},
computed :{
fullname : {
get : function() {
return this.firstName+" "+this.lastName;
}
}
}
});
</script>
</body>
</html>
我们定义了一个输入框,它绑定到fullname,这是一个计算属性。它返回一个名为get的函数,该函数给出全名,即名字和姓氏。此外,我们将名字和姓氏显示为 -
<h1>{{firstName}}</h1>
<h1>{{lastName}}</h1>
让我们在浏览器中检查一下。
现在,如果我们更改文本框中的名称,我们将看到以下屏幕截图中显示的名称没有反映相同的内容。
让我们在全名计算属性中添加 setter 函数。
<html>
<head>
<title>VueJs Instance</title>
<script type = "text/javascript" src = "js/vue.js"></script>
</head>
<body>
<div id = "computed_props">
<input type = "text" v-model = "fullname" />
<h1>{{firstName}}</h1>
<h1>{{lastName}}</h1>
</div>
<script type = "text/javascript">
var vm = new Vue({
el: '#computed_props',
data: {
firstName : "Terry",
lastName : "Ben"
},
methods: {
},
computed :{
fullname : {
get : function() {
return this.firstName+" "+this.lastName;
},
set : function(name) {
var fname = name.split(" ");
this.firstName = fname[0];
this.lastName = fname[1]
}
}
}
});
</script>
</body>
</html>
我们在全名计算属性中添加了 set 函数。
computed :{
fullname : {
get : function() {
return this.firstName+" "+this.lastName;
},
set : function(name) {
var fname = name.split(" ");
this.firstName = fname[0];
this.lastName = fname[1]
}
}
}
它以名称作为参数,只不过是文本框中的全名。随后,根据空间进行分割,并更新名字和姓氏。现在,当我们运行代码并编辑文本框时,浏览器中将显示相同的内容。由于设置功能,名字和姓氏将被更新。get 函数返回名字和姓氏,而 set 函数则更新它(如果有任何编辑)。
现在,文本框中输入的内容与显示的内容相匹配,如上面的屏幕截图所示。