This time I will bring you the difference between the use of computed and methods in Vue. What are the precautions for using computed and methods in Vue? The following is a practical case, let's take a look.
Computed in Vue can be used to simply splice the data that needs to be displayed
A simple example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>computed的使用</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>
<body>
<p id="root">
</p>
<script>
var vm = new Vue({
el: "#root",
data: {
name: "zhaozhao",
age: 13,
hobby: 'Python',
nameAgeStyle: {
fontSize: "20px",
color: "#0c8ac5"
}
},
template: `<p>
<p v-bind:style="nameAgeStyle">computed方式渲染: {{nameAndAge}}</p>
<p v-bind:style="nameAgeStyle">methods 方式渲染: {{getNameAndAge()}}</p>
<br>
<input type="text" v-model="hobby">
<p>爱好: {{hobby}}</p>
<p>{{noUse()}}</p>
</p>`,
computed: {
nameAndAge: {
get(){
console.log('调用computed');
return `${this.name} ==> ${this.age}`;
}
}
},
methods: {
getNameAndAge() {
console.log('调用methods');
return `${this.name} ==> ${this.age}`;
},
noUse(){
console.log("=methods==nouse==");
}
}
})
</script>
</body>
</html>jQuery Ajax verification user name steps detailed explanation
v-show adding expression steps detailed explanation
The above is the detailed content of The difference between using computed and methods in Vue. For more information, please follow other related articles on the PHP Chinese website!
The difference between static web pages and dynamic web pages
What is the difference between 4g and 5g mobile phones?
The difference between k8s and docker
The difference between JD.com's self-operated flagship store and its official flagship store
What is the copyright symbol
How to skip connecting to the Internet after booting up Windows 11
What are the registration-free spaces in China?
How to solve the problem that the hard disk partition cannot be opened