插值语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>姓名案例_插值语法实现</title>
<!-- 引入vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
姓:<input type="text" v-model="firstName">
<br>
名:<input type="text" v-model="lastName">
<br>
全名:<span>{{firstName.slice(0, 3)}}_{{lastName}}</span>
</div>
<script type="text/javascript">
// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
new Vue({
el: '#root',
data: {
firstName: '张',
lastName: '三'
}
})
</script>
</body>
</html>
methods(函数)实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>姓名案例_methods实现</title>
<!-- 引入vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
姓:<input type="text" v-model="firstName">
<br>
名:<input type="text" v-model="lastName">
<br>
全名:<span>{{fullName()}}</span>
</div>
<script type="text/javascript">
// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
new Vue({
el: '#root',
data: {
firstName: '张',
lastName: '三'
},
methods: {
fullName(){
return this.firstName.slice(0, 3) + '_' + this.lastName
}
}
})
</script>
</body>
</html>
计算属性实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>姓名案例_计算属性实现</title>
<!-- 引入vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<--
计算属性:
1.定义:要用的属性不存在,要通过vm已有的属性计算得来
2.原理:底层借助了 Object.defineproperty方法提供的getter 和 setter
3.get函数何时执行:
(1).初次读取时会执行一次
(2).当依赖的数据发生改变时会被再次调用
4.优势:与 methods实现相比,内部有缓存机制(复用),效率更高,调试方便
5.备注:
1.计算属性最终会出现在vm上,直接读取使用即可
2.如果计算属性要被修改,那必须写set函数去响应修改,且set中要引起计算时依赖的数据发生改变
-->
<body>
<div id="root">
姓:<input type="text" v-model="firstName">
<br>
名:<input type="text" v-model="lastName">
<br>
全名:<span>{{fullName}}</span>
</div>
<script type="text/javascript">
// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
new Vue({
el: '#root',
data: {
firstName: '张',
lastName: '三'
},
computed: {
fullName: {
// 当有人读取fullName时,get就会被调用,且返回值就作为fullName的值
/* get什么时候被调用?
1.初次读取fullName时
2.所依赖的数据发生变化时
*/
get(){
return this.firstName.slice(0, 3) + '_' + this.lastName
},
// get什么时候被调用? 当fullName被修改时
set(value){
const arr = value.split('_')
this.firstName = arr[0]
this.lastName = arr[1]
}
}
}
})
</script>
</body>
</html>
在确定计算属性只存在读取不存在修改时可以简写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>姓名案例_计算属性简写</title>
<!-- 引入vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<-- 当确定计算属性,只存在读取,不存在修改时,可以使用简写形式 -->
<body>
<div id="root">
姓:<input type="text" v-model="firstName">
<br>
名:<input type="text" v-model="lastName">
<br>
全名:<span>{{fullName}}</span>
</div>
<script type="text/javascript">
// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
new Vue({
el: '#root',
data: {
firstName: '张',
lastName: '三'
},
computed: {
// 完整写法
/* fullName: {
get(){
return this.firstName.slice(0, 3) + '_' + this.lastName
},
set(value){
const arr = value.split('_')
this.firstName = arr[0]
this.lastName = arr[1]
}
} */
// 简写
fullName(){
return this.firstName.slice(0, 3) + '_' + this.lastName
}
}
})
</script>
</body>
</html>