<!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 src="/vue.js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text" v-model="firstnName"><br>
名:<input type="text" v-model="lastName"><br>
全名:<span>{{firstnName.slice(0,3)}}-{{lastName}}</span>
<button @click="fullName()">点我</button>
</div>
</body>
<script>
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
new Vue({
el:'#root',
data:{
firstnName:'张',
lastName:'三'
}
})
</script>
</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>姓名案例_methods实现 </title>
<!-- 引入Vue -->
<script src="/vue.js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text" v-model="firstnName"><br>
名:<input type="text" v-model="lastName"><br>
全名:<span>{{fullName()}}</span>
</div>
</body>
<script>
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
new Vue({
el:'#root',
data:{
firstnName:'张',
lastName:'三'
},
methods:{
fullName(){
console.log('@----fullNmae')
return this.firstnName + '-' + this.lastName
}
}
})
</script>
</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>3.姓名案例_计算属性实现</title>
<!-- 引入Vue -->
<script src="/vue.js/vue.js"></script>
</head>
<body>
<!-- 计算属性:
1.定义:要用的属性不存在,要通过已有属性计算得米。
2.原理:底层借助了objcet.defineproperty方法提供的getter利lsetter.
3.get的数什么时候执行?
(1).初次读取时会执行一次。
(2).当依赖的数据发生改变时会被再次调用。
4.优势:与methods实现相比,内部有缓存机制(复用),效率更高,调试方便。
5.备注:
1.计算属性最终会出现在vm上,直接读取使用即可。
2.如果计算属性要被修改,那必须写set函数去响应修改,且set中要引起计算时依赖的数据发生改变
-->
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text" v-model="firstnName"><br/><br/>
名:<input type="text" v-model="lastName"><br/><br/>
测试:<input type="text" v-model="x"><br/><br/>
全名:<span>{{fullName}}</span>
</div>
</body>
<script>
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
const vm = new Vue({
el:'#root',
data:{
firstnName:'张',
lastName:'三',
x:'你好'
},
methods:{
demo(){
}
},
computed:{
fullName:{
//get有什么作用?当有人读取fullName时候,get就会被调用,且返回值就作为fullName的值
//get什么时候调用?1.初次读取fullName. 2.所依赖的数据发生变化时。
get(){
console.log('get被调用了')
// console.log(this)此处的this是vm
return this.firstnName + '-' + this.lastName
},
//set什么时候被调用?当fullName被修改时。
set(value){
console.log('set',value)
const arr = value.split('-')
this.firstnName = arr[0]
this.lastName = arr[1]
}
}
}
})
</script>
</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>3.姓名案例_计算属性实现</title>
<!-- 引入Vue -->
<script src="/vue.js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text" v-model="firstnName"><br/><br/>
名:<input type="text" v-model="lastName"><br/><br/>
全名:<span>{{fullName}}</span>
</div>
</body>
<script>
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
const vm = new Vue({
el:'#root',
data:{
firstnName:'张',
lastName:'三',
},
computed:{
//完整写法
// fullName:{
// get(){
// console.log('get被调用了')
// return this.firstnName + '-' + this.lastName
// },
// set(value){
// console.log('set',value)
// const arr = value.split('-')
// this.firstnName = arr[0]
// this.lastName = arr[1]
// }
// }
//简写
fullName:{
get(){
console.log('get被调用了')
return this.firstnName + '-' + this.lastName
},
}
}
})
</script>
</html>