Ⅰ 计算属性-监听属性
【一】计算属性:computed
计算属性是基于它们的依赖进行缓存的
计算属性只有在它的相关依赖发生改变时才会重新求值
计算属性就像Python中的property ,可以把方法/ 函数伪装成属性
- 1 如果使用计算属性,只要 计算属性中使用的变量,发生变化时,计算属性才重新运算
- 2 如果使用函数,只要页面发生变化,函数就会重新运算
- 3 计算属性当属性用,可以被for 循环,可以被v- if 判断
当属性用:
computed: { // 里面的值,当属性用
new_name( ) {
console. log( '执行了' )
return this. username. substring( 0 , 1 ) . toUpperCase( ) + this. username. substring( 1 )
}
}
【1】基本使用
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div id = " app" >
< h1> 用户输入英文名-首字母转大写</ h1>
< input type = " text" v-model = " username" > -->{{getUser()}}
< input type = " text" v-model = " username" > --->{{this.username.substring(0,1).toUpperCase()+this.username.substring(1)}}
< input type = " text" v-model = " username" > -->{{new_name}}
< hr>
< input type = " text" v-model = " age" > -->{{age}}
</ div>
</ body>
< script>
var vm = new Vue ( {
el : '#app' ,
data : {
username : '' ,
age : ''
} ,
methods : {
getUser ( ) {
console. log ( '-----执行了' )
return this . username. substring ( 0 , 1 ) . toUpperCase ( ) + this . username. substring ( 1 )
}
} ,
computed : {
new_name ( ) {
console. log ( '执行了' )
return this . username. substring ( 0 , 1 ) . toUpperCase ( ) + this . username. substring ( 1 )
}
}
} )
</ script>
</ html>
【2】实用计算属性重写过滤案例
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 过滤案例</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< input type = " text" v-model = " myText" >
< ul>
< li v-for = " item in newDataList" > {{item}}</ li>
</ ul>
</ div>
</ body>
< script>
var vm = new Vue ( {
el : '.app' ,
data : {
myText : '' ,
dataList : [ 'a' , 'at' , 'atom' , 'atommon' , 'be' , 'beyond' , 'cs' , 'csrf' ] ,
} ,
methods : {
} ,
computed : {
newDataList ( ) {
return this . dataList. filter ( item => {
if ( item. indexOf ( this . myText) >= 0 ) {
return true
} else {
return false
}
} )
}
}
} )
</ script>
</ html>
【二】监听属性:watch
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 监听属性</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 监听属性</ h1>
< button @click = " goods_type='食品'" > 食品</ button>
< button @click = " goods_type='蔬菜'" > 蔬菜</ button>
< button @click = " goods_type='冷藏'" > 冷藏</ button>
< hr>
{{goods_type}}
</ div>
</ body>
< script>
var vm = new Vue ( {
el : '.app' ,
data : {
goods_type : '所有数据'
} ,
watch : {
goods_type ( new_val, old_val ) {
console. log ( old_val)
console. log ( new_val)
console. log ( '向后端发送请求,过滤数据了' )
}
}
} )
</ script>
</ html>
Ⅱ 生命周期
- 过程中会有一些函数- - > 到某个过程时,就会触发这个函数的执行
- 8 个生命周期钩子函数
beforeCreate 创建 Vue实例/ 组件之 前调用
created 创建Vue实例成功后调用(可以在此处发送异步请求后端数据)
beforeMount 渲染DOM之前调用
mounted 渲染DOM之后调用
beforeUpdate 重新渲染之前调用(数据更新等操作时,控制DOM重新渲染)
updated 重新渲染完成之后调用
beforeDestroy 销毁之前调用
destroyed 销毁之后调用
- created会经常用:组件创建完成,就向后端发送ajax请求获取数据
- beforeDestroy 可能会用
【一】展示从创建开始到被销毁经历了一个过程
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 生命周期钩子</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 生命周期钩子</ h1>
{{name}}
< br>
< button @click = " name='跳大象舞'" > 点我换名字</ button>
</ div>
</ body>
< script>
var vm = new Vue ( {
el : '.app' ,
data : {
name : '蜡笔小新'
} ,
beforeCreate ( ) {
console. log ( '当前状态:beforeCreate' )
} ,
created ( ) {
console. log ( '当前状态:created' )
} ,
beforeMount ( ) {
console. log ( '当前状态:beforeMount' )
} ,
mounted ( ) {
console. log ( '当前状态:mounted' )
} ,
beforeUpdate ( ) {
console. log ( '当前状态:beforeUpdate' )
} ,
updated ( ) {
console. log ( '当前状态:updated' )
} ,
beforeDestroy ( ) {
console. log ( '当前状态:beforeDestroy' )
} ,
destroyed ( ) {
console. group ( '当前状态:destroyed' )
} ,
} )
</ script>
</ html>
【二】用一个组件展示从创建开始到被销毁经历了一个过程
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 生命周期钩子</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 组件生命周期</ h1>
< hr>
< child v-if = " show" > </ child>
< hr>
< button @click = " handleShow" > 点我隐藏显示组件</ button>
</ div>
</ body>
< script>
Vue. component ( 'child' , {
template : `
<div>
<h1>{{ title }}</h1>
<button @click="handleChange">点我换标题</button>
</div> ` ,
data ( ) {
return {
title : '我是child组件'
}
} ,
methods : {
handleChange ( ) {
this . title = '变了'
}
} ,
beforeCreate ( ) {
console. group ( '当前状态:beforeCreate' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
created ( ) {
console. group ( '当前状态:created' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
beforeMount ( ) {
console. group ( '当前状态:beforeMount' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
mounted ( ) {
console. group ( '当前状态:mounted' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . name)
} ,
beforeUpdate ( ) {
console. group ( '当前状态:beforeUpdate' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
updated ( ) {
console. group ( '当前状态:updated' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
beforeDestroy ( ) {
console. group ( '当前状态:beforeDestroy' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
destroyed ( ) {
console. group ( '当前状态:destroyed' )
console. log ( '当前el状态:' , this . $el)
console. log ( '当前data状态:' , this . $data)
console. log ( '当前name状态:' , this . title)
} ,
} )
var vm = new Vue ( {
el : '.app' ,
data : {
show : true
} ,
methods : {
handleShow ( ) {
this . show= ! this . show
}
}
} )
</ script>
</ html>
【三】生命周期钩子-组件-案例
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 生命周期钩子</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 组件生命周期</ h1>
< hr>
< child v-if = " show" > </ child>
< hr>
< button @click = " handleShow" > 点我隐藏显示组件</ button>
</ div>
</ body>
< script>
Vue. component ( 'child' , {
template : `
<div>
<div style="height: 500px;width: 200px;background-color: pink">
<p>我说:你好</p>
<p v-for="item in data_list">客服说:{{ item }}</p>
</div>
</div> ` ,
data ( ) {
return {
data_list : [ '欢迎光临' ] ,
t : null
}
} ,
created ( ) {
this . t = setInterval ( ( ) => {
console. log ( '开始追加拉' )
this . data_list. push ( '下次再来!!' )
} , 3000 )
} ,
beforeDestroy ( ) {
clearInterval ( this . t)
this . t = null
} ,
} )
var vm = new Vue ( {
el : '.app' ,
data : {
show : true
} ,
methods : {
handleShow ( ) {
this . show = ! this . show
}
}
} )
</ script>
</ html>
Ⅲ 组件
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 组件</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 组件</ h1>
< child> </ child>
< hr>
< chile3> </ chile3>
</ div>
</ body>
< script>
Vue. component ( 'child' , {
template : `
<div>
<div style="height: 200px;width: 100px;background-color: pink">
</div>
<chile2></chile2>
</div> ` ,
data ( ) {
return { }
} ,
components : {
'chile2' : {
template : `
<div>
<button>后退</button>
<div style="height: 200px;width: 100px;background-color: pink">
</div>
</div> ` ,
}
}
< ! -- -- >
} )
var obj = {
template : `
<div>
<div style="height: 200px;width: 100px;background-color: pink">
</div>
<chile2></chile2>
</div> ` ,
data ( ) {
return { }
} ,
components : {
'chile2' : {
template : `
<div>
<button>后退</button>
<div style="height: 200px;width: 100px;background-color: pink">
</div>
</div> ` ,
}
}
< ! -- -- >
}
Vue. component ( 'child' , obj)
var vm = new Vue ( {
el : '.app' ,
data : { } ,
methods : { } ,
components : {
'chile3' : {
template : `
<div>
<button>前进</button>
<div style="height: 200px;width: 100px;background-color: pink">
</div>
</div> ` ,
}
}
} )
var obj = {
template : `
<div>
<button>前进</button>
<div style="height: 200px;width: 100px;background-color: pink">
</div>
</div> ` ,
}
var vm = new Vue ( {
el : '.app' ,
data : { } ,
methods : { } ,
components : {
'chile3' : obj
}
} )
var vm = new Vue ( {
el : '.app' ,
data : { } ,
methods : { } ,
components : {
obj
}
} )
</ script>
</ html>
Ⅳ 组件通信之父传子
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 父传子---自定义属性</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 父传子---自定义属性</ h1>
< p> 这是父组件</ p>
< hr>
< child :mytitle = " title" :aa = " cc" :bb = " true" > </ child>
< hr>
</ div>
</ body>
< script>
Vue. component ( 'child' , {
template : `
<div>
<div style="height: 200px;width: 100px;background-color: pink">
<p>这是子组件</p>
<p>{{mytitle}}</p>
<p>{{aa}}</p>
</div>
</div> ` ,
data ( ) {
return { }
} ,
props : [ 'mytitle' , 'aa' ] ,
} )
var vm = new Vue ( {
el : '.app' ,
data : {
title : '给你的,儿子' ,
cc : 'zyb'
} ,
} )
</ script>
</ html>
Ⅴ 组件通信之子传父
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 子传父--自定义事件</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> 子传父--自定义事件</ h1>
< p> 这是父组件,子组件传过来的值是:{{name}}</ p>
< hr>
< child @send_data = " handleA" > </ child>
< hr>
</ div>
</ body>
< script>
Vue. component ( 'child' , {
template : `
<div>
<div style="height: 200px;background-color: pink">
<input type="text" v-model="username"> <button @click="handleSend">点我传给父亲</button>
<br>
{{username}}
</div>
</div> ` ,
data ( ) {
return {
username : ''
}
} ,
methods : {
handleSend ( ) {
this . $emit ( 'send_data' , this . username)
}
}
} )
var vm = new Vue ( {
el : '.app' ,
data : {
name : ''
} ,
methods : {
handleA ( name ) {
this . name= name
}
}
} )
</ script>
</ html>
Ⅵ ref属性
- 如果放在标签上
- 通过 this. $refs. 标签上的名字 拿到的值是 标签 对象
- 我们可以通过标签对象,修改 标签的属性。。。src属性,value属性
- 如果放在组件上
- 通过 this. $refs. 标签上的名字 拿到的值是 组件 对象
- 我们可以通过 组件对象,直接修改对象中得数据,直接调用对象中得方法
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> ref属性</ title>
< script src = " ./js2/vue.js" > </ script>
</ head>
< body>
< div class = " app" >
< h1> ref属性</ h1>
< img src = " ./img/6.jpg" alt = " " width = " 400px" height = " 400px" ref = " myimg" >
< br>
< input type = " text" ref = " myinput" >
< button @click = " handleClick" > 点我看控制台</ button>
< hr>
< child ref = " mychild" > </ child>
</ div>
</ body>
< script>
Vue. component ( 'child' , {
template : `
<div>
<div style="height: 200px;width: 100px;background-color: pink">
<p>这是子组件</p>
<p>{{name}}</p>
</div>
</div> ` ,
data ( ) {
return {
name : "zyb"
}
} ,
methods : {
handleA ( a ) {
console. log ( a)
this . name= a
}
}
} )
var vm = new Vue ( {
el : '.app' ,
data : {
name : '' ,
pice : '998'
} ,
methods : {
handleClick ( ) {
console. log ( this . $refs)
this . $refs. mychild. handleA ( this . pice)
}
}
} )
</ script>
</ html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY