一.案例代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<p>初始值:{{name}}</p>
<p>翻转值:{{reverseStr()}}</p>
<p>计算属性翻转值:{{reverse}}</p>
<button @click="setSeverseStr()">点击调用set方法</button>
</div>
<script type="text/javascript" src="js/jquery.js" ></script>
<script type="text/javascript" src="js/vue.js" ></script>
<script>
let vm=new Vue({
el:'#app',
data:{
name:'Rose Jack',
firstName:'小',
lastName:'明'
},
methods:{
reverseStr(){
return this.name.split('').reverse().join('');
},
setSeverseStr(){
this.reverse='xiao ming';
}
},
//计算选项
computed:{
reverse:{
//get方法
get(){
//return this.name.split('').reverse().join('');
return this.firstName+''+this.lastName;
},
//set方法
set(str){
let nameArr=str.split(' ');
this.firstName=nameArr[0];
this.lastName=nameArr[1];
alert(this.firstName);
alert(this.lastName);
}
}
}
})
</script>
</body>
</html>
二.结果